通过反射动态调用外部托管Dll

添加命名空间:Using System.Reflection

 1         public object  InvokeExternDll(string DllPath,string ClassName,string FunctionName,object[] ObjArray_Parameter)
 2         {
 3             object objReturnValue = new object();
 4 
 5             //加载DLL文件
 6             Assembly assembly = Assembly.LoadFile(DllPath);
 7 
 8             //获取所需类的类型 格式:"namespaceName.className"
 9             Type classType = assembly.GetType(ClassName);
10 
11             //建立此类型的对象
12             Object specificObj = Activator.CreateInstance(classType);
13 
14             //获取类中的想要执行的方法
15             MethodInfo ExecuteMethod = classType.GetMethod(FunctionName);
16 
17             //调用函数
18             objReturnValue = ExecuteMethod.Invoke(specificObj, ObjArray_Parameter);
19 
20             return objReturnValue;
21         }

反射加载数据的用法:

Load("DllName")  //dll 名称无后缀,从当前目录中加载

LoadFie(DllPath) //加载Dll完整路径 从一个指定文件中加载程序集

LoadFrom(DllPath) //加载Dll完整路径的程序集 同时加载目标程序集所引用和依赖的其他程序集

原文地址:https://www.cnblogs.com/YourDirection/p/12124610.html