using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PTMedInsuInterface.Helper; using PTMedInsuInterface.Variables; namespace PTMedInsuInterface.Common { #region class DllInvoke { public static object ManagedInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams) { //Global.writeLog("程序集信息:" + NameSpace + "." + ClassName + "." + MethodName); //Global.writeLog("文件全路径:" + DllFileName); //Global.writeLog("命名空间.类.方法:" + NameSpace + "." + ClassName + "." + MethodName); //Global.writeLog("入参:" + ObjArrayParams[0].ToString()); object o = new object(); try { //载入程序集 Assembly DllAssembly = Assembly.LoadFile(DllFileName); string err; if (DllAssembly == null) { err ="DllAssembly未加载成功"; return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err); } Type DllType = DllAssembly.GetType(NameSpace + "." + ClassName); if (DllType == null) { err = "DllType 未获取到"; return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err); } //查找要调用的方 法并进行调用 MethodInfo MyMethod = DllType.GetMethod(MethodName); if (MyMethod != null) { object mObject = Activator.CreateInstance(DllType); try { if (mObject == null) { err = " mObject is not exsit"; return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err); } if (ObjArrayParams == null) { o = MyMethod.Invoke(mObject, null); } else { o = MyMethod.Invoke(mObject, new object[] { ObjArrayParams[0].ToString() }); } } catch (Exception e) { string inMes = "", outMes = e.Message; ; if (e.InnerException != null) { inMes = e.InnerException.Message; outMes += inMes; } o = JsonHelper.setExceptionJson(-1, $"调用{DllFileName},命名空间:{NameSpace},类名:{ClassName},方法名:{MethodName}异常", outMes); return o; } } else { err = "MyMethod 未获取成功!"; return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err); } } catch (Exception mEx) { Global.writeLog(mEx.ToString()); string inMes = "", outMes = mEx.Message; if (mEx.InnerException != null) { inMes = mEx.InnerException.Message; outMes = outMes + inMes; } o = JsonHelper.setExceptionJson(-1, "DLL名:" + DllFileName + ",命名空间:" + NameSpace +",类名:" + ClassName + ",方法名:" + MethodName + ",调用(ManagedInvoke)异常:" , outMes); } //GlobalVariables.writeLog("出参:" + o.ToString()); return o; } } #endregion }