2
$\begingroup$

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 = 
  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.

Examples/Failures

1

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.

2

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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