ComValue.SetValueFromCreateGenericInstance

This method creates an instance of a generic type on the ComValue structure by allowing you to specify a generic type name, the generic type parameters (as a ComArray) and optional constructor parameters (as a ComArray.)

o.ComValue.SetValueFromCreateGenericInstance(lcGenericType,loTypes,loParms)

Parameters

lcGenericType
The generic type to access without the brackets and generic type names.
Example: System.Collections.Generic.List

loTypes
A ComArray of the generic type parameters. Types need to be fully qualified (like System.String)

loParms
A ComArray of parameters passed to the constructor. This parameter is not optional and cannot be null.

Remarks

Generic Types are inaccessible directly from FoxPro

Generic types cannot be accessed directly in FoxPro - any attempt to access a generic type directly will likely cause FoxPro to crash. Always use intermediate methods like InvokeMethod(), GetProperty() and SetProperty() to interact with any generic types or values.

Example

loBridge = GetwwDotnetBridge()

loValue = loBridge.CreateComValue()

*** Create an array of strings for generic types
loGenericTypes = loBridge.CreateArray("System.String")
loGenericTypes.AddItem("System.String")   && Generic parm for List<string>

*** Create a new array of List Items 
loList = loBridge.CreateArray("System.String")
loList.AddItem("New Item 1")
loList.AddItem("New Item 2")

*** Add the array as parameter to the constructor
loParms = loBridge.CreateArray("System.Object")
loParms.AddItem(loList)  && parameter to List<string>

*** Create List<string>(list)  on the ComValue structure
*** Note: loParms must be passed and can't be NULL
loValue.SetValueFromCreateGenericInstance("System.Collections.Generic.List",loGenericTypes,loParms) 
? loValue.Value   && Generic List<string>

? loBridge.GetProperty(loValue.Value,"Count")  && 2

*** Add a new item with Collection.Add Method
? loBridge.InvokeMethod(loValue.Value,"Add","New Item 3 (code)")
? loBridge.GetProperty(loValue.Value,"Count")  && 3

*** Access with Indexers
loItem = loBridge.GetProperty(loValue,"Value[0]")
? loItem
loItem = loBridge.GetProperty(loValue,"Value[2]")
? loItem

*** Access as Enumerable
loList = loBridge.CreateArray("System.String")
loList.FromEnumerable(loValue.Value)

? "---"
? loList.Item(0)
? loList.Item(1)
? loList.Item(2)

See also:

Class ComValue

© West Wind Technologies, 2023 • Updated: 11/07/18
Comment or report problem with topic