[IronPython] assembly context binding in appDomain and .NET interop

Dino Viehland dinov at microsoft.com
Wed Sep 15 16:26:42 PDT 2010


The usual solution to this is to do the add reference from the C# host rather than from IronPython.  You can do that with:

engine.Runtime.LoadAssembly(typeof(Entity.TestPy).Assembly);

And then just get rid of the sys.path.append/clr.AddReferenceToFile.

There are some similar questions on StackOverflow as well if you want to vote up the answers :):

http://stackoverflow.com/questions/3009213/c-ironpython-interop-with-shared-c-class-library/3011147#3011147 [perfect match]
http://stackoverflow.com/questions/3305792/ironpython-returns-wrong-type-when-using-class-in-classy-library/3309873#3309873 [very similar]


From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of kevin won
Sent: Wednesday, September 15, 2010 4:09 PM
To: users at lists.ironpython.com
Subject: [IronPython] assembly context binding in appDomain and .NET interop

New to the list & hoping an IronPython expert might be able to shed some light on an vexing IronPython runtime problem I'm having.

I've actually posted this question in StackOverflow to no avail here: http://stackoverflow.com/questions/2785252/c-4-0-casting-dynamic-to-static there is a lot more details about this problem there.

essentially what I'm having problems with I think boils down to an assembly binding context mismatch. However, I have totally run out of ideas on how to resolve the problem.

I have a C# assembly I reference in IronPython. I new up an object defined in that assembly in IronPython:





sys.path.append(r'C:..path here...')


clr.AddReferenceToFile("entity.dll")


import Entity.TestPy


def GetTest():


    return Entity.TestPy(); // the C# class


So now over in C#, I call this and assign it to a dynamic var:




// C# code calling IronPython and getting back an object



dynamic test = scriptEngine.GetTest();


Entity.TestPy t = test  as Entity.TestPy; // t==null!!!

the cast fails. However, if I do this with a BCL type (say System.Uri as an example), this cast succeeds. It only fails for types in my own assembly.

It has been suggested to me that the reason for this type mismatch is that the runtime takes in the totality of the runtime environment when considering if types match. It appears that the IronPython runtime AppDomain gets a different Assembly Load Context than the C# (http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx) and thus even though the Entity.TestPy type in C# and IP are the same, because the context is different, the types do not line up and therefore the cast fails.

I don't want a dynamic type--I want the static type, but I can't figure out how to get the CLR to be happy about this.

While I'm strongly suspicious that the Assembly Load Context is what is the problem--I don't know that for sure. I'm guessing an IronPython guru can help sort this out.

thanks

Kevin












-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20100915/1474a2f7/attachment.htm>


More information about the Users mailing list