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)

Return Value

nothing

Parameters

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

loTypes
A ComArray of string values that name the generic type parameters. This is the type T definition in List<T> or an implementation like List<string> for example where the generic parameter would be System.String. Types need to be always fully qualified (ie System.String,MyApp.MyClass).

loParms
Values: NULL or ComArray of Parameters
An optional list of constructor parameters for the generic type. If no parameters are passed pass .NULL. For any parameters pass a ComArray with one item for each parameter.

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() or a ComValuestructure to interact with any generic types or values.

Example

CLEAR
do wwDotNetBridge
LOCAL loBridge as wwDotNetBridge
loBridge = GetwwDotnetBridge()

*** We'll build a value of: List<TestCustomer>

*** This is the generic type we want to create for list items
lcGenericItemType = "Westwind.WebConnection.TestCustomer"

*** We need a ComArray of strings, to hold the Generic Type Name(s)
loGenericTypes = loBridge.Createarray("System.String")
loGenericTypes.Add(lcGenericItemType)

*** As of 8.0.1 you can pass NULL if there are no parameters
*** Use this is possible as this makes i
* loParameters = null

*** Optional CTOR Parameters - CANNOT BE NULL prior to v8.0.1 (or method won't resolve via Reflection)
loParameters = loBridge.CreateArray("System.Object")  && Empty CTOR parameter list

*** Most .NET objects don't have CTOR parameters so you can skip code up to the loValue= block

*** But if you want to specify CTOR parameters you can. You need a ComArray and add each
*** each parameter as an item. Here we pre-initialize the List from an array
*** which is one of the List<T> parameter options: List<T>(IEnumerable items)  - (array is IEnumerable)

*** Create an array of items to initialize with
loItems = loBridge.CreateArray(lcGenericItemType)

*** Add individual items to the array
loCust = loBridge.CreateInstance(lcGenericItemType)
loCust.Name = "Rick 1"
loItems.Add(loCust)

loCust = loBridge.CreateInstance(lcGenericItemType)
loCust.Name = "Janet 1"
loItems.Add(loCust)

*** Now use the item list as the one parameter for the List CTOR
loParameters.Add(loItems)

*** Create our ComValue (use with InvokeMethod or SetProperty)
loValue = loBridge.CreateComValue()

*** Create the list instance
loValue.SetValueFromCreateGenericInstance("System.Collections.Generic.List",
                                              loGenericTypes, loParameters)

*** Now add items - first get a COMARRAY list
loList = loValue.GetValue()

*** Create instances and add
loCust = loBridge.CreateInstance(lcGenericItemType)
loCust.Name = "Rick"
loList.Add(loCust)

loCust = loBridge.CreateInstance(lcGenericItemType)
loCust.Name = "Janet"
loList.Add(loCust)

*** List of the ComValue reflects added items  
? loBridge.ToJson(loValue,.T.)    && should show 4 items 

See also:

Class ComValue

© West Wind Technologies, 1996-2024 • Updated: 08/26/24
Comment or report problem with topic