如何动态加载从先前接口构建的程序集?

时间:2018-10-09 20:51:57

标签: c# .net interface

我有一个.net Winform App,它试图动态加载程序集,但遇到了ReflectionTypeLoadException异常

该应用程序包含对P.DLL和C.DLL的两个引用(下面的代码) 然后动态加载SI.DLL(要加载的代码在下面)

这是之前我的问题开始出现的代码

P.DLL包含此

public interface iP
{
    string Version { get; }
}

public class P : iP
{
    public P()
    {
        Version = "4.0";
    }

    #region iP Implementation
    public string Version { get; private set; }
    #endregion
}

C.DLL包含此

//
// C.DLL
//
public interface iC : iP
{
    // no interface changes here
}
public class C : P, iC
{
    public C()
    {
        // this class does stuff but is irrelevant to problem except it is the inheritance chain
    }
}

SI.DLL包含此

//
// 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),而C源自P(接口iP)

SI (iSI) : C (iC) : P (iP)

因此,通过以上代码,我可以动态加载SI.DLL,并且没有问题。

这是我的应用程序中的代码,可动态加载SI.DLL

这是调用加载程序的代码:

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;
    }

更改

我通过添加一个新接口(从旧接口继承而来)更改了P.DLL,并在P类中实现了该功能:

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
}

我也更改了C.DLL以使用新接口的派生:

public interface iC : iP4
{
    // no interface changes here
}

随着P和C的更改:

SI(接口iSI)源自C(接口iC),而C(接口iC 4

SI (iSI) : C (iC) : P (iP4)

我进行了更改,重新编译了P.DLL和C.DLL及其应用程序。 现在,当我运行该应用程序时,我尝试加载使用 OLD iP 接口构建的SI.DLL实例,并且出现异常。

我的问题是如何动态加载从先前接口构建的程序集?

0 个答案:

没有答案