tags:

views:

167

answers:

1

Have an IronPython package named "Entities". That package contains an "Entity.py" file that defines a "Customer" class and an "Address" class.

If I run this program:

customer = Customer()
print customer.GetType().AssemblyQualifiedName
address = Address()
print address.GetType().AssemblyQualifiedName

I get this output:

IronPython.NewTypes.System.Object_1$1, Snippets.scripting, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
IronPython.NewTypes.System.Object_1$1, Snippets.scripting, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

How does that work? Why do both types have the same type name (Object_1$1)?

I have to use an API that requires the Assembly Qualified Name for a type to create instances of that type. I'd like to be able to use it this way:

customer = aFactory.Create("Entities.Customer, Entities");

How will I do that? Is there any way to specify the namespace and assembly name?

Thanks

+2  A: 

You want to look into clrtype which is new in IronPython 2.6. It enables you to control the type which is emitted and there's some good samples out there with pre-baked solutions one of which might work here.

The reason the types are the same is that IronPython only needs to do so much with the .NET type system. That includes overriding any virtual methods on the base type and implementing any interfaces which you've requested by including them in your bases list. Other than that all the types are identical and they're really defined by dynamic lookups into their PythonType object. So for example in Object_1$1 we define an override of "ToString" which will look up in the current PythonType's resolution order to see if ToString has been defined and if so we invoke that. This enables the object's PythonType members to change at runtime and even allows the actual python type to change at runtime. Also if we didn't do something like this defining classes would leak memory because .NET types are not collectible.

Dino Viehland
@Dino: Thanks for your answer. I read the link you provided. It says that "The ClrType sample available in the IronPython website shows how to build on top of the __clrtype__ hook." I can't find that sample and Google can't either. Will you send me the link to that sample please?
Sly
Sample is available here: http://ironpython.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=12482#DownloadId=96609
Dino Viehland
@Dino: Thanks, I'll check that.
Sly