8

我试图学习如何将 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();
}

我遇到的问题是,即使

type.FullName

包含正确的值(“TestDLL.PrintString”等),

线

myDll.CreateInstance(type.FullName) as Action

总是返回 null。

我不完全确定问题是什么,或者我如何解决它。

如示例所示,我希望能够将新的 Action 实现添加到 DLL,并让主程序在每个实现上调用 DoAction(),而无需重新编译原始程序。希望这是有道理的!

4

2 回答 2

3

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

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

你应该把它包装在一个 try/catch 块中。当您编写 Action 时(因为您没有为您的程序集设置引用),就像在 中一样List<Action>,这个 Action 是指Action 委托。

于 2013-06-29T01:05:30.830 回答
2

很可能您Action在主程序集和“其他”程序集中都定义了,并且您正在转换到错误的程序集中。

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

有关详细信息,请参阅:无法通过程序集的自定义属性获取类型

于 2013-06-28T22:59:21.823 回答