/// <summary> /// 动态调用DLL /// </summary> /// <param name="DllFileName">dll名称</param> /// <param name="NameSpace">命名空间</param> /// <param name="ClassName">类名</param> /// <param name="MethodName">方法名</param> /// <param name="ObjArrayParams">参数数组</param> /// <returns></returns> public static object DllInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, params object[] ObjArrayParams) { try { //载入程序集 Assembly DllAssembly = Assembly.LoadFrom(DllFileName); Type[] DllTypes = DllAssembly.GetTypes(); foreach (Type DllType in DllTypes) { //查找要调用的命名空间及类 if (DllType.Namespace == NameSpace && DllType.Name == ClassName) { //查找要调用的方法并进行调用 MethodInfo MyMethod = DllType.GetMethod(MethodName); if (MyMethod != null) { object mObject = Activator.CreateInstance(DllType); return(MyMethod.Invoke(mObject, ObjArrayParams)); } } } } catch (Exception) { //MessageBoxHelper.ShowError(mEx.Message); } return((object)0); }
/// <summary> /// 加载DLL的方法 不支持重载方法 速度慢 /// </summary> /// <example> /// <code> /// Safe.DllInvoke("../bin/Pub.Class.dll".GetMapPath(), "Pub.Class", "Session2", "Set", new object[] { "test", "3" }); /// Msg.Write(Safe.DllInvoke("../bin/Pub.Class.dll".GetMapPath(), "Pub.Class", "Session2", "Get", new object[] { "test" })); /// </code> /// </example> /// <param name="DllFileName">dll全路径</param> /// <param name="NameSpace">命名空间</param> /// <param name="ClassName">类名</param> /// <param name="MethodName">方法名</param> /// <param name="ObjArrayParams">参数</param> /// <returns>返回值</returns> public static object DllInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams) { Assembly DllAssembly = Assembly.LoadFrom(DllFileName); Type[] DllTypes = DllAssembly.GetTypes(); foreach (Type DllType in DllTypes) { if (DllType.Namespace == NameSpace && DllType.Name == ClassName) { MethodInfo MyMethod = DllType.GetMethod(MethodName); if (MyMethod.IsNotNull()) { object mObject = Activator.CreateInstance(DllType); return(MyMethod.Invoke(mObject, ObjArrayParams)); } } } return((object)0); }