We are on the Wolfram Finance Platform using NET/Link to grab data from a C# API. We have a list of Core.Data.Securities.Options that we need to upcast to Core.Data.Securities.Security. Working with some great people on here yesterday, we were able to piece together how to cast the .NET list. However, it takes about 41 seconds.
options = NETNew["System.Collections.Generic.List`1[Core.Data. Securities.Security]"]; Scan[options@ Add[CastNETObject[#, "Core.Data.Securities.Security"]] &, optionChain@Options@ToArray]; client@GetMarketDataSnapshots[options];
optionChain@Options gets a System.Collections.Generic.List`1[Core.Data.Securities.Option] object (this can be very, very long). Our GetMarketDataSnapshots only accepts a System.Collections.Generic.List`1[Core.Data.Securities.Security] object.
We tried some other methods of casting before finding the above working code.
Map[CastNETObject[#, "Core.Data.Securities.Security"]&, optionChain@Options@ToArray];
Could not cast System.Collections.Generic.List`1[Core.Data.Securities.Option] to System.Collections.Generic.List`1[Core.Data.Securities.Security].
This baffled me for a long time, because it's not an unreasonable cast. I believe it had something to do with trying to put Security objects back into the List. Creating a new List object, like in the current code, squelched the error.
CastNETObject::netexcptn: A .NET exception occurred: System.InvalidCastException: Specified cast is not valid. at Wolfram.NETLink.Internal.CallPacketHandler.cast(KernelLinkImpl ml). >>
I didn't really expect this one to work. It was during the desperate times. Adding it here, in case anyone has the same idea.
Find a way to cast objects in a .NET list in Wolfram in the fastest way possible.