适用于.NET的Python:在多个版本中使用相同的.NET程序集 [英] Python for .NET: Using same .NET assembly in multiple versions

查看:85
本文介绍了适用于.NET的Python:在多个版本中使用相同的.NET程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我有2个版本的程序集,并希望在我的Python项目中同时使用它们.

My problem: I have an assembly in 2 versions and want to use them at the same time in my Python project.

.NET库安装在GAC(MSIL)中,具有相同的公共令牌:

The .NET libs are installed in GAC (MSIL), having the same public token:

lib.dll (1.0.0.0)
lib.dll (2.0.0.0)

在Python中,我想要这样的东西:

In Python I want something like that:

import clr
clr.AddReference("lib, Version=1.0.0.0, ...")
from lib import Class
myClass1 = Class()
myClass1.Operation()

*magic*

clr.AddReference("lib, Version=2.0.0.0, ...")
from lib import class
myClass2 = Class()
myClass2.Operation()
myClass2.OperationFromVersion2()

*other stuff*

# both objects should be accessibly
myClass1.Operation() 
myClass2.OperationFromVersion2()

有没有办法做到这一点?带有AppDomains或bindingRedirect的东西吗?

Is there a way to do that? Something with AppDomains or bindingRedirect?

注意:当然,myClass1.operationFromVersion2()可能会失败...

Note: Of course myClass1.operationFromVersion2() can fail...

推荐答案

好了,我找到了一个解决方案:适用于.NET的Python也支持反射!

Well I found a solution: Python for .NET also supports Reflection!

代替

clr.AddReference("lib, Version=1.0.0.0, ...")

您必须使用

assembly1 = clr.AddReference("lib, Version=1.0.0.0, ...")

通过该程序集,您可以像在C#中一样使用所有Reflection东西.在我的示例中,我必须使用以下代码(与版本2相同):

With that assembly you can use all the Reflection stuff like in C#. In my example I have to use following code (same for version 2):

from System import Type
type1 = assembly1.GetType(...)
constructor1 = type1.GetConstructor(Type.EmptyTypes)
myClass1 = constructor1.Invoke([])

这篇关于适用于.NET的Python:在多个版本中使用相同的.NET程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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