IronPython Exception: [A]Person cannot be cast to [B]Person
Had an interesting problem while using the Hosting API the other day. Here's the exception message.
[A]CSharpClass.Person cannot be cast to [B]CSharpClass.Person. Type A originates from 'CSharpClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '[project directory]\bin\Debug\CSharpClass.dll'. Type B originates from 'CSharpClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location '[temp directory]\CSharpClass.DLL'.
I was struggling with this for a while until I found this link. In order to get my IronPython scripts to communicate with my CLR-based code, I was using the clr module's AddReference methods. These methods require you to point at the appropriate copy of the library you want to consume. I do stress "copy". Let's say you point your IronPython script at the bin\debug folder of a C# library project you're consuming in a console application. When you run the application, you'll get the exception message above. To make this example work, you'd have to point your IronPython script at the bin\Debug folder of your console application instead. Clearly, this is a maintenance nightmare. Instead, insert the following chunk of code into your hosting application (in the example described above, your console application) to avoid the problem all together.
Assembly assembly = typeof (CSharpClass.Person).Assembly;
runtime.LoadAssembly(assembly);
Labels: .NET, Hosting, IronPython






0 Comments:
Post a Comment
<< Home