<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> </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> </div>
<div>The error/issue I'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'm able to cast these objects in the Watch Window, but only when the code actually runs it fails with exception as below:</div>
<div> Microsoft.Scripting.ArgumentTypeException: Expected DLRProto1_Extensions.Test03.CastTestObject, got DLRProto1_Extensions.Test03.CastTestObject</div>
<div> Unable to cast object of type 'DLRProto1_Extensions.Test03.CastTestObject' to type 'DLRProto1_Extensions.Test03.CastTestObject'.</div>
<div> </div>
<div>I won't include the initialization of the engine and scope as they have been taken care by the base class.</div>
<div> </div>
<div>// C# library</div>
<div> public class CastTestObject<br> {<br> private int cnt;<br> private int limit;</div>
<div> public CastTestObject()<br> {<br> cnt = 0;<br> limit = 1000;<br> }</div>
<div> public void IncrementCount()<br> {<br> cnt++;<br> } </div>
<div> }</div>
<div> </div>
<div># Python script</div>
<div>import clr<br>clr.AddReferenceToFile("DLRProto1_Extensions.dll")</div>
<div>from DLRProto1_Extensions.Test03 import CastTestObject<br>cto = CastTestObject()<br>print cto</div>
<div>def returnTestObject():<br> cto2 = CastTestObject()<br> return cto2</div>
<div> </div>
<div>// C# code manipulation below</div>
<div>string script = ReadScript("Scenarios\\test03.py");<br>CompileAndExecute(script);</div>
<div> </div>
<div>CastTestObject ctoObject;</div>
<div>// does not work, but works in Watch Window in Debug Mode</div>
<div>ctoObject = ScrScope.GetVariable<CastTestObject>("cto");</div>
<div> </div>
<div>// cast does not work, but works in Watch Window in Debug Mode</div>
<div>object cObject = ScrEngine.Execute("returnTestObject()", ScrScope);<br>ctoObject = (CastTestObject) cObject; //fails here with the exception above</div>
<div> </div>
<div>Any help is greatly appreciated.</div>
<div> </div>
<div>Thank you.</div>
<div> </div>
<div>Samuel</div>