ReflectionHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace WWPipeLine.Commons
  6. {
  7. /// <summary>
  8. /// 动态库反射
  9. /// </summary>
  10. public class ReflectionHelper
  11. {
  12. /// <summary>
  13. /// 反射静态方法调用
  14. /// </summary>
  15. /// <param name="dllFileName">DLL的存放路径</param>
  16. /// <param name="className">类名</param>
  17. /// <param name="methodName">方法名</param>
  18. /// <param name="parameters">方法参数列表</param>
  19. /// <returns></returns>
  20. public static object GetStaticMethodObj(string dllFileName, string className, string methodName, object[] parameters)
  21. {
  22. Assembly assembly = Assembly.LoadFrom(dllFileName);
  23. if (assembly == null)
  24. {
  25. return null;
  26. }
  27. Type getType = assembly.GetType(className);
  28. if (getType == null)
  29. {
  30. return null;
  31. }
  32. MethodInfo methodInfo = getType.GetMethod(methodName);
  33. if (methodInfo == null)
  34. {
  35. return null;
  36. }
  37. return methodInfo.Invoke(null, parameters);
  38. }
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. /// <param name="dllFileName">DLL的存放路径</param>
  43. /// <param name="className">类名</param>
  44. /// <param name="singleName">单例名称</param>
  45. /// <param name="methodName">方法名</param>
  46. /// <param name="parameters">参数列表</param>
  47. /// <returns></returns>
  48. public static object GetSignleMethodObj(string dllFileName, string className, string singleName, string methodName, object[] parameters)
  49. {
  50. Assembly assembly = Assembly.LoadFrom(dllFileName);
  51. if (assembly == null)
  52. {
  53. return null;
  54. }
  55. Type type = assembly.GetType(className);
  56. if (type == null)
  57. {
  58. return null;
  59. }
  60. PropertyInfo info = type.GetProperty(singleName);
  61. if (info == null)
  62. {
  63. return null;
  64. }
  65. object obj = info.GetValue(null, null);
  66. if (obj == null)
  67. {
  68. return null;
  69. }
  70. return ExecutionMethod(obj, type, methodName, parameters);
  71. }
  72. /// <summary>
  73. /// 普通类反射调用方法
  74. /// </summary>
  75. /// <param name="dllFileName">库名</param>
  76. /// <param name="className">类名</param>
  77. /// <param name="methodName">方法名称</param>
  78. /// <param name="parameters">参数</param>
  79. /// <returns></returns>
  80. public static object GetComMethodObj(string dllFileName, string className, string methodName, object[] parameters)
  81. {
  82. Assembly assembly = Assembly.LoadFrom(dllFileName);
  83. if (assembly == null)
  84. {
  85. return null;
  86. }
  87. object obj = assembly.CreateInstance(className);
  88. if (obj == null)
  89. {
  90. return null;
  91. }
  92. Type type = obj.GetType();
  93. if (type == null)
  94. {
  95. return null;
  96. }
  97. return ExecutionMethod(obj, type, methodName, parameters);
  98. }
  99. /// <summary>
  100. /// 已知对象,执行相应的函数
  101. /// </summary>
  102. /// <param name="executionObj">执行对象</param>
  103. /// <param name="type">执行对象对应的类型</param>
  104. /// <param name="methodName">执行对象的需要执行的函数</param>
  105. /// <param name="parameters">参数</param>
  106. /// <returns></returns>
  107. public static object ExecutionMethod(object executionObj, Type type, string methodName, object[] parameters)
  108. {
  109. if (type == null)
  110. {
  111. return null;
  112. }
  113. //MethodInfo info = type.GetMethod.GetMethod(methodName);
  114. MethodInfo mathodGetAll = null;
  115. var mAll = type.GetMethods();
  116. var mInfos = mAll.Where(f => f.Name == methodName);
  117. if (mInfos.Count() == 1)
  118. {
  119. mathodGetAll = mInfos.First();
  120. }
  121. else
  122. {
  123. if (mAll != null && mAll.Any())
  124. {
  125. mathodGetAll = mAll.FirstOrDefault(f => f.Name == methodName && f.GetParameters().Count() == parameters?.Count());
  126. }
  127. }
  128. if (mathodGetAll == null)
  129. {
  130. return null;
  131. }
  132. var temParameters = mathodGetAll.GetParameters();
  133. object[] objectParamList = null;
  134. Type[] typeArray;
  135. if (mathodGetAll.GetParameters().Length > 0)
  136. {
  137. if (parameters == null)
  138. {
  139. parameters = new object[0];
  140. }
  141. typeArray = new Type[temParameters.Length];
  142. ParameterInfo[] infoArray3 = mathodGetAll.GetParameters();
  143. object[] objArray2 = new object[infoArray3.Length];
  144. for (int i = 0; i < infoArray3.Length; i++)
  145. {
  146. if (i > parameters.Length - 1)
  147. {
  148. objArray2.SetValue(infoArray3[i].DefaultValue, i);
  149. }
  150. else if (parameters[i] == null)
  151. {
  152. objArray2.SetValue(parameters[i], i);
  153. }
  154. else
  155. {
  156. if (infoArray3[i].ParameterType.Name == "Object")
  157. {
  158. objArray2.SetValue(parameters[i], i);
  159. typeArray.SetValue(infoArray3[i].ParameterType, i);
  160. }
  161. else
  162. {
  163. JsonSerializer serializer = new JsonSerializer();
  164. string s = SerializeHelper.JsonHepler.Json_SerializeObject(parameters[i], true);
  165. var temPtype = parameters[i].GetType();
  166. object obj4 = serializer.Deserialize(new JsonTextReader(new System.IO.StringReader(s)), infoArray3[i].ParameterType.IsClass ? (infoArray3[i].ParameterType.Name == temPtype.Name ? infoArray3[i].ParameterType : temPtype) : infoArray3[i].ParameterType);
  167. objArray2.SetValue(obj4, i);
  168. if (obj4 != null)
  169. {
  170. typeArray.SetValue(infoArray3[i].ParameterType, i);
  171. }
  172. }
  173. }
  174. }
  175. objectParamList = objArray2;
  176. }
  177. object infoDatas = null;
  178. try
  179. {
  180. return infoDatas = mathodGetAll.Invoke(executionObj, objectParamList);
  181. }
  182. catch (Exception ex)
  183. {
  184. if (ex.InnerException != null)
  185. {
  186. if (ex.InnerException.Message.Contains("System.ServiceModel.Channels.ServiceChannel"))
  187. {
  188. throw new Exception("WCF服务无法连接!", ex);
  189. }
  190. if (ex.InnerException.Message.Contains("Connection open error"))
  191. {
  192. throw new Exception("数据库连接失败!", ex);
  193. }
  194. }
  195. LogHelper.Error(ex); return null;
  196. }
  197. }
  198. /// <summary>
  199. /// 创建类对象
  200. /// </summary>
  201. /// <param name="dllFileName">库名</param>
  202. /// <param name="className">类名</param>
  203. /// <returns></returns>
  204. public static object GetObjectFromDLL(string dllFileName, string className)
  205. {
  206. Assembly assembly = Assembly.LoadFrom(dllFileName);
  207. if (assembly == null)
  208. {
  209. return null;
  210. }
  211. return assembly.CreateInstance(className);
  212. }
  213. }
  214. }