"Unable to cast" error when passing object of custom Class via SetGlobal
Author: jheffez
Creation Date: 6/24/2017 8:22 AM
profile picture

jheffez

#1
I'm unable to pass a class object from one strategy to another via Set/Getlobal. On the receiving strategy when doing getglobal it errors with:

[A]Mypkw cannot be cast to [B]Mypkw. Type A originates from 'q2qvrhyo, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array. Type B originates from 'ujqouhkn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array.

"Setglobal" Strategy:
CODE:
Please log in to see this code.



"Getglobal" Strategy:
CODE:
Please log in to see this code.


Both strategies have the class definition (also tried placing class to the namespace section):
CODE:
Please log in to see this code.
profile picture

Eugene

#2
I see you're using Visual Studio since Wealth-Lab's compiler would return a cropped error message instead of the complete one in your message. This will make it easier for you to understand explanation and to apply solution.

Problem is, types are per-assembly. Each strategy is compiled independently (recall the requirement to reopen the Strategy Monitor if you modify a strategy the SM is running since SM works with its own compiled copy?) and loaded into the AppDomain. Although your strategies are loaded into the same AppDomain, they're compiled into different DLLs with different Guids by the WealthScriptCompiler. So there are in fact two different Mypkw classes. Hence, the compiler cannot match the Mypkw class from assembly #1 to eponymous class from assembly #2 despite similarity in their name and properties to our human eyes.

Solution: make your Mypkw class public and place it in an external DLL. Then you can refer to it as if it were in the GAC, recognizable to any class that calls it.
profile picture

jheffez

#3
It worked!
Thanks!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).