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.

Current Code

options = 

      "Core.Data.Securities.Security"]] &, 


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[optionChain@Options, "System.Collections.Generic.List`1[Core.Data.Securities.Security]"]

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.

End Goal

Find a way to cast objects in a .NET list in Wolfram in the fastest way possible.


1 Answer 1


After thinking about it for a while, I realized that the best, least messy, and fastest solution was to create a helper library for situations like this.

namespace CastHelper
    public class CastList
        public static List<Security> CastListToSecurity(List<Option> castee)
            List<Security> caster = castee.Select(x => x as Security).ToList();

            return caster;

Then I added this on the Wolfram Side after loading the NET Assembly and NET Type.

options = CastList`CastListToSecurity[optionChain@Options]

Cut down the casting time from 40 seconds to .005 seconds! That should be fast enough for our purposes.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.