<div>Forgive me if this has been asked before. I already went through the archive and could not find any answer to this.</div>
<div>&nbsp;</div>
<div>Basically, the idea is to be able to define a class in C#.</div>
<div>Then in Python, make a reference to the C# library and instantiate an object based on this class.</div>
<div>Then as requested by C# through IronPython, this object reference is then passed back to be used/manipulated in C#.</div>
<div>&nbsp;</div>
<div>The error/issue I&#39;m having is the casting of the object (passed from Python) to the C# class. (last part of the code below)</div>
<div>Funny thing is that when I put a breakpoint on Visual Studio, I&#39;m&nbsp;able to cast these objects in the Watch Window, but only when the code actually runs it fails with exception as below:</div>
<div>&nbsp;&nbsp;&nbsp; Microsoft.Scripting.ArgumentTypeException: Expected DLRProto1_Extensions.Test03.CastTestObject, got DLRProto1_Extensions.Test03.CastTestObject</div>
<div>&nbsp;&nbsp;&nbsp; Unable to cast object of type &#39;DLRProto1_Extensions.Test03.CastTestObject&#39; to type &#39;DLRProto1_Extensions.Test03.CastTestObject&#39;.</div>
<div>&nbsp;</div>
<div>I won&#39;t include the initialization of the engine and scope as they have been taken care by the base class.</div>
<div>&nbsp;</div>
<div>// C# library</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp; public class CastTestObject<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int cnt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int limit;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public CastTestObject()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cnt = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; limit = 1000;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void IncrementCount()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cnt++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div># Python script</div>
<div>import clr<br>clr.AddReferenceToFile(&quot;DLRProto1_Extensions.dll&quot;)</div>
<div>from DLRProto1_Extensions.Test03 import CastTestObject<br>cto = CastTestObject()<br>print cto</div>
<div>def returnTestObject():<br>&nbsp;cto2 = CastTestObject()<br>&nbsp;return cto2</div>
<div>&nbsp;</div>
<div>//&nbsp;C# code manipulation below</div>
<div>string script = ReadScript(&quot;Scenarios\\test03.py&quot;);<br>CompileAndExecute(script);</div>
<div>&nbsp;</div>
<div>CastTestObject ctoObject;</div>
<div>// does not work, but works in Watch Window in Debug Mode</div>
<div>ctoObject = ScrScope.GetVariable&lt;CastTestObject&gt;(&quot;cto&quot;);</div>
<div>&nbsp;</div>
<div>// cast does not work, but works in Watch Window in Debug Mode</div>
<div>object cObject = ScrEngine.Execute(&quot;returnTestObject()&quot;, ScrScope);<br>ctoObject = (CastTestObject) cObject; //fails here with the exception above</div>
<div>&nbsp;</div>
<div>Any help is greatly appreciated.</div>
<div>&nbsp;</div>
<div>Thank you.</div>
<div>&nbsp;</div>
<div>Samuel</div>