InvokeMethod.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using PTMedInsuInterface.Helper;
  12. using PTMedInsuInterface.Variables;
  13. namespace PTMedInsuInterface.Common
  14. {
  15. #region
  16. class DllInvoke
  17. {
  18. public static object ManagedInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams)
  19. {
  20. //Global.writeLog("程序集信息:" + NameSpace + "." + ClassName + "." + MethodName);
  21. //Global.writeLog("文件全路径:" + DllFileName);
  22. //Global.writeLog("命名空间.类.方法:" + NameSpace + "." + ClassName + "." + MethodName);
  23. //Global.writeLog("入参:" + ObjArrayParams[0].ToString());
  24. object o = new object();
  25. try
  26. {
  27. //载入程序集
  28. Assembly DllAssembly = Assembly.LoadFile(DllFileName);
  29. string err;
  30. if (DllAssembly == null)
  31. {
  32. err ="DllAssembly未加载成功";
  33. return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err);
  34. }
  35. Type DllType = DllAssembly.GetType(NameSpace + "." + ClassName);
  36. if (DllType == null)
  37. {
  38. err = "DllType 未获取到";
  39. return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err);
  40. }
  41. //查找要调用的方 法并进行调用
  42. MethodInfo MyMethod = DllType.GetMethod(MethodName);
  43. if (MyMethod != null)
  44. {
  45. object mObject = Activator.CreateInstance(DllType);
  46. try
  47. {
  48. if (mObject == null)
  49. {
  50. err = " mObject is not exsit";
  51. return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err);
  52. }
  53. if (ObjArrayParams == null)
  54. {
  55. o = MyMethod.Invoke(mObject, null);
  56. }
  57. else
  58. {
  59. o = MyMethod.Invoke(mObject, new object[] { ObjArrayParams[0].ToString() });
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. string inMes = "", outMes = e.Message; ;
  65. if (e.InnerException != null)
  66. {
  67. inMes = e.InnerException.Message;
  68. outMes += inMes;
  69. }
  70. o = JsonHelper.setExceptionJson(-1, $"调用{DllFileName},命名空间:{NameSpace},类名:{ClassName},方法名:{MethodName}异常", outMes);
  71. return o;
  72. }
  73. }
  74. else
  75. {
  76. err = "MyMethod 未获取成功!";
  77. return JsonHelper.setExceptionJson(-1, "ManagedInvoke", err);
  78. }
  79. }
  80. catch (Exception mEx)
  81. {
  82. Global.writeLog(mEx.ToString());
  83. string inMes = "", outMes = mEx.Message;
  84. if (mEx.InnerException != null)
  85. {
  86. inMes = mEx.InnerException.Message;
  87. outMes = outMes + inMes;
  88. }
  89. o = JsonHelper.setExceptionJson(-1, "DLL名:" + DllFileName + ",命名空间:" + NameSpace +",类名:" + ClassName + ",方法名:" + MethodName + ",调用(ManagedInvoke)异常:" , outMes);
  90. }
  91. //GlobalVariables.writeLog("出参:" + o.ToString());
  92. return o;
  93. }
  94. }
  95. #endregion
  96. }