C# 如何动态加载从以前的接口生成的程序集?
我有一个.net Winform应用程序正在尝试动态加载程序集,但收到ReflectionTypeLoadException异常 该应用程序包括对p.DLL和C.DLL的两个引用(代码如下) 然后动态加载SI.DLL(要加载的代码如下) 这是我的问题开始之前的代码 p.DLL包含此 C.DLL包含此 SI.DLL包含此 从本质上说,SI(接口iSI)源于C(接口iC),而C(接口iC)源于p(接口iP) 因此,通过上面的代码,我可以动态加载SI.DLL,并且没有问题 这是我的应用程序中动态加载SI.DLL的代码 以下是调用加载程序的代码:C# 如何动态加载从以前的接口生成的程序集?,c#,.net,interface,C#,.net,Interface,我有一个.net Winform应用程序正在尝试动态加载程序集,但收到ReflectionTypeLoadException异常 该应用程序包括对p.DLL和C.DLL的两个引用(代码如下) 然后动态加载SI.DLL(要加载的代码如下) 这是我的问题开始之前的代码 p.DLL包含此 C.DLL包含此 SI.DLL包含此 从本质上说,SI(接口iSI)源于C(接口iC),而C(接口iC)源于p(接口iP) 因此,通过上面的代码,我可以动态加载SI.DLL,并且没有问题 这是我的应用程序中动态加载S
bool bLoaded = false;
iC retval = LoadPlugin<iC>(fullPathAndFileName, out bLoaded);
我已将C.DLL更改为使用“从新接口派生”:
public interface iC : iP4
{
// no interface changes here
}
p和C的更改:
SI(接口iSI)源于C(接口iC),C(接口iC)源于p(接口iP4)
我做了更改,重新编译了p.DLL和C.DLL以及我的应用程序。
现在,当我运行应用程序时,我尝试加载使用旧iP接口构建的SI.DLL实例,结果出现异常
我的问题是如何动态加载从以前的接口构建的程序集?我认为“最好”的方法是对旧的iP和iC接口进行版本设置,并将它们全部加载。然后,在编写iSI时,您可以从适当的版本继承,并且没有任何问题。不过,您可以查看MEF,看看您是否能够找出如何动态选择程序集的哪个版本:一个旧示例:Cory-对接口/DLL进行版本化不是有损于接口的用途吗?嗯,有点。。但使用同一界面的旧版本也是如此。我总是不喜欢假设人们的用例,但是如果你可以访问所有3个用例,那么你就会想重新编译它们。但是,如果您试图允许第三方创建插件,则需要避免更改接口,或者以允许两个版本运行的方式进行设计。
//
// SI.DLL
//
public interface iSI : iC
{
// no interface changes here
}
public class SI_Engine : C, iSI
{
public SI_Engine()
{
// this is the object in the SI.DLL assembly that I am trying to load
}
}
SI (iSI) : C (iC) : P (iP)
bool bLoaded = false;
iC retval = LoadPlugin<iC>(fullPathAndFileName, out bLoaded);
public static T LoadPlugin<T>(string fullPathAndFileName, out bool loaded)
{
loaded = false;
T retval = default(T);
Assembly assembly = Assembly.LoadFile(fullPathAndFileName);
if (assembly != null)
{
Type pluginType = typeof(T);
// this is where I am getting the ReflectionTypeLoadException
Type[] types = assembly.GetTypes();
// Method 'TimeTrial' in type 'SI.SI_Engine'
// from assembly 'SI, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null'
// does not have an implementation.
//
foreach (Type type in types)
{
if (type.IsInterface || type.IsAbstract)
{
continue;
}
else
{
if (type.GetInterface(pluginType.FullName) != null)
{
retval = (T)Activator.CreateInstance(type);
loaded = true;
break;
}
}
}
}
return retval;
}
public interface iP
{
string Version { get; }
}
public interface iP4 : iP
{
bool TimeTrial();
}
public class P : iP4
{
public P()
{
Version = "4.0";
}
#region iP Implementation
public string Version { get; private set; }
#endregion
#region iP4 Implementation
public bool TimeTrial()
{
return Version == "4.0";
}
#endregion
}
public interface iC : iP4
{
// no interface changes here
}
SI (iSI) : C (iC) : P (iP4)