views:

44

answers:

2

Can someone recommend a workaround for this ironpython bug?

I have a class contained within an external class library. I consume this class inside an embedded ironpython instance. When the class is retrieved from the scope by my c# app, the classes don't seem to match up!

My python script:

import sys
import clr
from ExternalAssembly import *
from IronPythonBug import *

internalClass = InternalClass("internal")
externalClass = ExternalClass("external")

My c# app:

internalClass = scope.GetVariable("internalClass");
externalClass = scope.GetVariable("externalClass");

if (internalClass is InternalClass)
    Console.WriteLine("IternalClass matches");
else
    Console.WriteLine("Error: InternalClass does not match");

if (externalClass is ExternalClass)
    Console.WriteLine("ExternalClass matches");
else
    Console.WriteLine("Error: ExternalClass does not match");

Console output:

IternalClass matches
Error: ExternalClass does not match

feel free to download a project that illustrates this bug: http://www.virtual-chaos.net/zip/IronPythonBug.zip

A: 

Do externalClass.GetType() and inspect the properties.

Seeing InternalClass is also from the same assembly, compare that type with the above one too.

leppie
When I inspect externalClass.GetType(), the properties look the same as when I check GetType() on an instance created in my C# app. However, if I try to match the c# and python instances, they will fail. Whereas two instances created in c# will have matching Type objects: ExternalClass external1 = new ExternalClass("one"); ExternalClass external2 = new ExternalClass("two"); externalClass.GetType() == external1.GetType() //this would fail external1.GetType() == external2.GetType() //this would work
djmc
I just had a peek at your code, and everything looks OK. Perhaps you should ask on the IronPython mailing list.
leppie
cool, good call. tks for checkin the code out.
djmc
+5  A: 

This is caused by CLR loader contexts. The call to Assembly.LoadFile loads another copy of the assembly into a different context - giving you a duplicate set of types but with different identities. Instead of using Assembly.LoadFile to get the assembly object use typeof(ExternalClass).Assembly.

Dino Viehland
thank you so much. that solved the problem exactly. I really appreciate the advice.
djmc