<div>Sweet. I loaded them using the LoadAssembly in C# and voila the casting works.</div>
<div> </div>
<div>Thanks Curt for the prompt reply.</div>
<div> </div>
<div>Samuel<br></div>
<div class="gmail_quote">On Thu, Jan 22, 2009 at 11:18 AM, Curt Hagenlocher <span dir="ltr"><<a href="mailto:curt@hagenlocher.org">curt@hagenlocher.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">The error "unable to cast object of type X to type X" is generally the result of an assembly loader issue. If you're using the hosting interface and want to get a reference to the hosting assembly, don't use "clr.AddReferenceToFile" -- you'll end up with the same assembly loaded twice. I think "clr.AddReference" might be okay, but the best choice is for the hosting C# code to inject itself directly into the ScriptRuntime by saying something like this:
<div><br></div>
<div>runtime.LoadAssembly(typeof(CastTestObject).Assembly)</div>
<div><br>
<div class="gmail_quote">
<div>
<div></div>
<div class="Wj3C7c">On Thu, Jan 22, 2009 at 8:59 AM, Samuel Tjokrosoesilo <span dir="ltr"><<a href="mailto:samuelt@gmail.com" target="_blank">samuelt@gmail.com</a>></span> wrote:<br></div></div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div>
<div></div>
<div class="Wj3C7c">
<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><font color="#888888">
<div>Samuel</div></font><br></div></div>_______________________________________________<br>Users mailing list<br><a href="mailto:Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br><a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
<br></blockquote></div><br></div><br>_______________________________________________<br>Users mailing list<br><a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br><a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
<br></blockquote></div><br>