Using FromEnumerable() to access Generic Collections
Generic types cannot be accessed directly via COM from FoxPro. Any attempt to access members of a generic type will cause FoxPro to crash with a physical C5 error.
A common result from .NET methods are generic lists like List<T>
and these lists - unlike arrays - are returned to FoxPro in their raw form. In order to access these lists you can convert them into ComArray instances with the following code:
*** Method that returns a Generic List<Employees>
loEmps = loBridge.InvokeMethod( loTax, "GetList")
? loEmps && (Object) (this is the generic type)
*** Load Generic List into ComArray as Enumerable
loItems = loBridge.CreateArray()
loItems.FromEnumerable(loEmps)
*** Now you can use ComArray methods as usual
For i = 0 to loItems.Count-1
loEmp = loItems.Item(i)
? i
? loEmp.Name
? loEmp.Amount
EndFor
.FromEnumerable
creates a new array from the generic list or any other type that implements IEnumerable
. The array adds the same items of the list and thus makes the ComArray behave as you would expect.
FromEnumerable Overhead
FromEnumerable()
essentially makes a copy of the enumerable list so there's overhead both in iterating over the input list as well as creating a new array that references each of the elements in the original collection. If usingFromEnumerable()
from a non-materialized data source it will effectively materialize all items.For this reason make sure you don't use this method on very large collections as it may be slow and use excessive amounts of memory.
© West Wind Technologies, 2023 • Updated: 08/25/18
Comment or report problem with topic