这是一个与我问的另一个问题相关的分支问题here https://stackoverflow.com/questions/2783616/net-4-0-dynamic-object-used-statically/。我将其分开,因为这实际上是一个子问题:
我在投射类型对象时遇到困难dynamic
到另一个(已知的)静态类型。
我有一个ironPython 脚本正在执行此操作:
import clr
clr.AddReference("System")
from System import *
def GetBclUri():
return Uri("http://google.com")
请注意,这只是新建 BCL System.Uri 类型并返回它. So 我知道静态类型返回的对象。
现在在 C# 领域,我正在更新托管内容的脚本并调用此 getter 来返回 Uri 对象:
dynamic uri = scriptEngine.GetBclUri();
System.Uri u = uri as System.Uri; // casts the dynamic to static fine
工作没问题。我现在可以使用强类型 Uri 对象,就像它最初是静态实例化的一样。
然而....
现在我想定义我自己的 C# 类,该类将在动态领域中更新,就像我对 Uri 所做的那样。我的简单 C# 类:
namespace Entity
{
public class TestPy // stupid simple test class of my own
{
public string DoSomething(string something)
{
return something;
}
}
}
现在在 Python 中,新建一个该类型的对象并返回它:
sys.path.append(r'C:..path here...')
clr.AddReferenceToFile("entity.dll")
import Entity.TestPy
def GetTest():
return Entity.TestPy(); // the C# class
然后在 C# 中调用 getter:
dynamic test = scriptEngine.GetTest();
Entity.TestPy t = test as Entity.TestPy; // t==null!!!
在这里,演员阵容不起作用。请注意,“测试”对象(动态)是有效的——我可以调用 DoSomething()——它只是不会转换为已知的静态类型
string s = test.DoSomething("asdf"); // dynamic object works fine
所以我很困惑。 BCL 类型 System.Uri 将从动态类型转换为正确的静态类型,但我自己的类型不会。显然有一些我不明白的事情......
--
更新:我做了很多测试,以确保我的程序集引用全部正确排列。我更改了引用的程序集版本号,然后查看了dynamic
C# 中的对象 GetType() 信息——它是正确的版本号,但它仍然不会转换回已知的静态类型。
然后,我在控制台应用程序中创建了另一个类来检查是否会得到相同的结果,结果是积极的:我可以获得dynamic
在 C# 中引用在我的 Python 脚本中实例化的静态类型,但它不会正确地转换回已知的静态类型。
--
更多信息:
Anton 在下文中指出,AppDomain 程序集绑定上下文可能是罪魁祸首。经过一些测试后,我认为很有可能是这样。 。 。但我不知道如何解决它!我不知道程序集绑定上下文,因此感谢 Anton,我对程序集解析和其中出现的微妙错误有了更多的了解。
因此,在启动脚本引擎之前,我通过在 C# 中对事件放置一个处理程序来观察程序集解析过程。这让我可以看到 python 引擎启动并且运行时开始解析程序集:
private static Type pType = null; // this will be the python type ref
// prior to script engine starting, start monitoring assembly resolution
AppDomain.CurrentDomain.AssemblyResolve
+= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
...并且处理程序设置 var pType到 python 正在加载的类型:
static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
{
if (args.LoadedAssembly.FullName ==
"Entity, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null")
{
// when the script engine loads the entity assembly, get a reference
// to that type so we can use it to cast to later.
// This Type ref magically carries with it (invisibly as far as I can
// tell) the assembly binding context
pType = args.LoadedAssembly.GetType("Entity.TestPy");
}
}
因此,虽然 python 使用的类型在 C# 中是相同的,但我认为(如 Anton 所提议的)不同的绑定上下文意味着对于运行时来说,两种类型(“加载绑定上下文”中的一种类型和'loadfrom 绑定上下文)是不同的——所以你不能转换到另一个。
现在我已经掌握了由 Python 加载的类型(及其绑定上下文),你瞧,在 C# 中我可以将动态对象转换为这个静态类型,并且它可以工作:
dynamic test = scriptEngine.GetTest();
var pythonBoundContextObject =
Convert.ChangeType(test, pType); // pType = python bound
string wow = pythonBoundContextObject .DoSomething("success");
但是,叹息,这并不能完全解决问题,因为 varpythonBoundContextObject
虽然类型正确,仍然带有错误的程序集绑定上下文的污点。这意味着我无法将其传递给代码的其他部分,因为我们仍然有这个奇怪的类型不匹配绑定上下文的无形幽灵让我感到冷漠。
// class that takes type TestPy in the ctor...
public class Foo
{
TestPy tp;
public Foo(TestPy t)
{
this.tp = t;
}
}
// can't pass the pythonBoundContextObject (from above): wrong binding context
Foo f = new Foo(pythonBoundContextObject); // all aboard the fail boat
因此,解决方案必须在 Python 方面:让脚本加载到正确的程序集绑定上下文中。
在Python中,如果我这样做:
# in my python script
AppDomain.CurrentDomain.Load(
"Entity, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null");
运行时无法解析我的类型:
import Entity.TestPy #fails