名称空间和程序集名称如何用于IronPython类型? [英] How namespace and assembly names work for IronPython types?

查看:59
本文介绍了名称空间和程序集名称如何用于IronPython类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有一个名为实体"的IronPython程序包.该程序包包含一个"Entity.py"文件,该文件定义了"Customer"类和"Address"类.

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

如果我运行此程序:

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

我得到以下输出:

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

这是如何工作的?为什么两种类型都具有相同的类型名称(Object_1$1)?

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

我必须使用要求类型的程序集合格名称的API来创建该类型的实例.我希望能够以这种方式使用它:

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?

谢谢

推荐答案

您要研究

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.

类型相同的原因是IronPython只需要对.NET类型系统做很多事情.这包括覆盖基本类型上的所有虚拟方法,并通过将它们包括在基本列表中来实现您所请求的任何接口.除此之外,所有类型都是相同的,并且它们实际上是通过对PythonType对象的动态查找来定义的.因此,例如在Object_1 $ 1中,我们定义了"ToString"的替代,它将按当前PythonType的解析顺序进行查找,以查看是否已定义ToString,如果是,则调用它.这使对象的PythonType成员可以在运行时更改,甚至允许实际的python类型在运行时更改.同样,如果我们不做这样的事情,那么定义类会泄漏内存,因为.NET类型是不可收集的.

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.

这篇关于名称空间和程序集名称如何用于IronPython类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆