想实现一个动态加载 DLL 的功能,实现插件化开发。求推荐方案。语言 C#。

比如我们做一个扩展的接口。让其他多个第三方应用,实现这个接口,然后发 dll 给我们。放在某个目录,直接可以动态的调用这个 dll 中的方法。有什么好的方案推荐吗? thanks !

阅读 3.7k
1 个回答

参考 "How to: Load Assemblies into an Application Domain"
https://docs.microsoft.com/en...

using System;
using System.Reflection;

public class Asmload0
{
    public static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进