C# 动态加载DLL中的接口

C# 动态加载DLL中的接口,c#,dll,interface,C#,Dll,Interface,我试图学习如何将DLL动态加载到C程序中。其思想是DLL将包含一个接口和接口的几个不同实现,这样,如果我想添加新的实现,我就不必重新编译整个项目 所以我创建了这个测试。这是我的DLL文件: namespace TestDLL { public interface Action { void DoAction(); } public class PrintString : Action { public void DoAction() {

我试图学习如何将DLL动态加载到C程序中。其思想是DLL将包含一个接口和接口的几个不同实现,这样,如果我想添加新的实现,我就不必重新编译整个项目

所以我创建了这个测试。这是我的DLL文件:

namespace TestDLL
{
  public interface Action
  {
    void DoAction();
  }

  public class PrintString : Action
  {
    public void DoAction()
    {
      Console.WriteLine("Hello World!");
    }
  }

  public class PrintInt : Action
  {
    public void DoAction()
    {
      Console.WriteLine("Hello 1!");
    }
  }
}
在我的主程序中,我试着做这样的事情:

static void Main(string[] args)
{
  List<Action> actions = new List<Action>();
  Assembly myDll = Assembly.LoadFrom("TestDLL.dll");
  Type[] types = myDll.GetExportedTypes();

  for (int i = 0; i < types.Length; i++)
  {
    Type type = types[i];
    if (type.GetInterface("TestDLL.Action") != null  && type != null)
    {
        Action new_action = myDll.CreateInstance(type.FullName) as Action;
        if (new_action != null)
          actions.Add(new_action);
        else
          Console.WriteLine("New Action is NULL");
    }
  }

  foreach (Action action in actions)
    action.DoAction();
}
包含正确的值(“TestDLL.PrintString”等)

线路

myDll.CreateInstance(type.FullName) as Action
始终返回null

我不太清楚问题是什么,也不知道如何解决


与示例中一样,我希望能够向DLL中添加新的Action实现,并让主程序在每个实现上调用DoAction(),而无需重新编译原始程序。希望这有意义

您的
操作很可能在主程序集和“其他”程序集中都定义了,并且您正在向错误的程序集进行转换

通常,共享接口在单独的程序集(“SDK”)中定义,并与主应用程序和插件程序集链接。通过源共享接口不起作用,因为类的标识包括程序集名称和类型名称


有关更多信息,请参见:

通过您的主要实现,您最好这样做

            List<object> actions = new List<object>();
            Assembly myDll = Assembly.LoadFrom("TestDLL.dll");
            Type[] types = myDll.GetTypes();

            for (int i = 0; i < types.Length; i++)
            {
                Type type = myDll.GetType(types[i].FullName);
                if (type.GetInterface("TestDLL.Action") != null)
                {
                    object obj = Activator.CreateInstance(type);
                    if (obj != null)
                        actions.Add(obj);
                }
            }

            foreach (var action in actions)
            {
                MethodInfo mi = action.GetType().GetMethod("DoAction");
                mi.Invoke(action, null);
            }
List actions=new List();
Assembly myDll=Assembly.LoadFrom(“TestDLL.dll”);
Type[]types=myDll.GetTypes();
for(int i=0;i
你应该用try/catch块把它包起来。
当您编写操作时(因为您没有对程序集设置引用),如在
列表中,此操作引用操作委托。

您在这里使用的
操作作为操作
,在哪里定义?您是用第一段代码引用程序集,还是声明了两次
操作
,一次在从TestDLL.dll加载的程序集中,一次在主项目中?
            List<object> actions = new List<object>();
            Assembly myDll = Assembly.LoadFrom("TestDLL.dll");
            Type[] types = myDll.GetTypes();

            for (int i = 0; i < types.Length; i++)
            {
                Type type = myDll.GetType(types[i].FullName);
                if (type.GetInterface("TestDLL.Action") != null)
                {
                    object obj = Activator.CreateInstance(type);
                    if (obj != null)
                        actions.Add(obj);
                }
            }

            foreach (var action in actions)
            {
                MethodInfo mi = action.GetType().GetMethod("DoAction");
                mi.Invoke(action, null);
            }