【发布时间】:2011-11-19 20:06:47
【问题描述】:
在我的情况下,有三个组件:Consumer 类、IExposedIface 接口和实现IExposedIface 的Exposed 类。 Consumer 和 Exposed 都与 IExposedIface 静态链接,但 Consumer 没有对 Exposed 的编译时引用。
我正在尝试提出一个方案,该方案允许Consumer 在运行时加载不同版本的Exposed(取决于输入数据 - 假设每个输入文档都包含有关 Exposed 应该是哪个版本的信息用来处理它)。为了实现这一点,我开始研究 AppDomains,现在我有了一个可以运行的基本版本。
到目前为止,在我看来,将IExposedIface 程序集提供给Exposed 程序集有两种选择。
仅在
Consumer的 bin 目录中拥有IExposedIface.dll并为AppDomain处理AppDomain.AssemblyResolve事件,我将在其中创建Exposed的实例在
Consumer的bin 目录以及每个Exposed.dll旁边都有IExposedIface.dll。
现在考虑我针对这个IExposedIface 构建Exposed:
public interface IExposedIface
{
string SaySomething();
}
我针对这个IExposedIface 构建Consumer:
public interface IExposedIface
{
string SaySomething();
string SaySomethingDifferent();
}
在第一种情况下,例外
例外:方法 'SaySomethingDifferent' 在类型 'Exposed.Exposed' 中来自 程序集 'Exposed, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 没有实现。
在我调用appDomain.CreateInstanceAndUnwrap(...) 以在新创建的AppDomain 中创建Exposed 的实例时抛出。
我觉得这很合理。
但在第二种情况下,appDomain.CreateInstanceAndUnwrap(...) 运行良好,我可以毫无问题地在检索到的对象上调用“SaySomething()”方法。一个例外
在接口/类型上找不到方法“SaySomethingDifferent”“IExposedIface.IExposedIface, IExposedIface, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null”。
仅当我在Consumer 中实际调用SaySomethingDifferent() 时才抛出。
我很惊讶在第二种情况下 CLR 让我走了这么远......有人可以解释为什么这是可能的吗?
【问题讨论】:
标签: c# .net remoting appdomain .net-assembly