终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

245 lines
7.7 KiB

  1. using BPASmartClient.MessageCommunication.MsgControl.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPASmartClient.MessageCommunication.MsgControl
  10. {
  11. public class TypeHelper
  12. {
  13. public Type M_GetTypeByName(string filepath,string TypeName)
  14. {
  15. try
  16. {
  17. if (!File.Exists(filepath))
  18. {
  19. return null;
  20. }
  21. Assembly assembly = Assembly.LoadFrom(filepath);
  22. Type[] types = assembly.GetTypes();
  23. foreach (Type type in types)
  24. {
  25. if (type.Name == TypeName)
  26. {
  27. return type;
  28. }
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. Exception ex2 = ex;
  34. return null;
  35. }
  36. return null;
  37. }
  38. public Type M_GetTypeByFullName(string filepath,string TypeFullName)
  39. {
  40. try
  41. {
  42. if (!File.Exists(filepath))
  43. {
  44. return null;
  45. }
  46. Assembly assembly = Assembly.LoadFrom(filepath);
  47. Type[] types = assembly.GetTypes();
  48. foreach (Type type in types)
  49. {
  50. if (type.FullName == TypeFullName)
  51. {
  52. return type;
  53. }
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. Exception ex2 = ex;
  59. return null;
  60. }
  61. return null;
  62. }
  63. public object M_GetPropertyValue(object obj,string Str_PropertyName)
  64. {
  65. Type type = obj.GetType();
  66. PropertyInfo property = type.GetProperty(Str_PropertyName);
  67. if (null != property)
  68. {
  69. return property.GetValue(obj,null);
  70. }
  71. return null;
  72. }
  73. public void M_SetFieldInfoValue(object Target_obj,string Str_PropertyName,object obj_Value)
  74. {
  75. Type type = Target_obj.GetType();
  76. FieldInfo field = type.GetField(Str_PropertyName);
  77. field.SetValue(Target_obj,obj_Value);
  78. }
  79. public FieldInfo[] M_Get_PublicFieldInfos(Type type)
  80. {
  81. return type.GetFields();
  82. }
  83. public FieldInfo[] M_Get_AllFieldInfosByObject(object obj)
  84. {
  85. Type type = obj.GetType();
  86. return type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
  87. }
  88. public FieldInfo[] M_Get_AllFieldInfos(Type type)
  89. {
  90. return type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
  91. }
  92. public Type M_GetPropertyType(object Target_obj,string Str_PropertyName)
  93. {
  94. Type type = Target_obj.GetType();
  95. PropertyInfo property = type.GetProperty(Str_PropertyName);
  96. return property.PropertyType;
  97. }
  98. public MethodInfo[] M_GetMethodInfos(Type userType)
  99. {
  100. return userType.GetMethods();
  101. }
  102. public List<方法描述> M_GetMethodInfosByName(Type userType,string str_MethedName)
  103. {
  104. List<方法描述> list = new List<方法描述>();
  105. MethodInfo[] methods = userType.GetMethods();
  106. foreach (MethodInfo methodInfo in methods)
  107. {
  108. if (methodInfo.Name == str_MethedName)
  109. {
  110. 方法描述 方法描述 = new 方法描述();
  111. 方法描述.Obj_Type = userType;
  112. 方法描述.M_MethodInfo = methodInfo;
  113. 方法描述.M_ParameterInfos = methodInfo.GetParameters();
  114. list.Add(方法描述);
  115. }
  116. }
  117. return list;
  118. }
  119. public 方法描述 M_GetMethodInfosByNameandParmarTypes(Type userType,string str_MethedName,Type[] parmerTypes)
  120. {
  121. //IL_002b: Unknown result type (might be due to invalid IL or missing references)
  122. MethodInfo method;
  123. try
  124. {
  125. method = userType.GetMethod(str_MethedName,parmerTypes);
  126. }
  127. catch (Exception ex)
  128. {
  129. //MessageBox.Show("对" + userType.FullName + "执行: M_GetMethodInfosByNameandParmarTypes发生异常," + ex.Message);
  130. return null;
  131. }
  132. if (method == null)
  133. {
  134. return null;
  135. }
  136. 方法描述 方法描述 = new 方法描述();
  137. 方法描述.Obj_Type = userType;
  138. 方法描述.M_MethodInfo = method;
  139. 方法描述.M_ParameterInfos = method.GetParameters();
  140. return 方法描述;
  141. }
  142. public object M_Call_InstancMethod(object objInstance,string str_MethodName,object[] obj_Parmeters)
  143. {
  144. MethodInfo method;
  145. if (obj_Parmeters != null)
  146. {
  147. Type[] array = new Type[obj_Parmeters.Count()];
  148. for (int i = 0; i < obj_Parmeters.Count(); i++)
  149. {
  150. array[i] = obj_Parmeters[i].GetType();
  151. }
  152. method = objInstance.GetType().GetMethod(str_MethodName,array);
  153. return method.Invoke(objInstance,obj_Parmeters);
  154. }
  155. method = objInstance.GetType().GetMethod(str_MethodName,new Type[0]);
  156. return method.Invoke(objInstance,null);
  157. }
  158. public EventInfo[] M_GetEvents(Type userType)
  159. {
  160. return userType.GetEvents();
  161. }
  162. public void M_AddEventProcess()
  163. {
  164. }
  165. public PropertyInfo[] M_GetPropertyInfos(Type userType)
  166. {
  167. return userType.GetProperties();
  168. }
  169. public PropertyInfo[] M_GetPropertyInfos(object userObject)
  170. {
  171. return userObject.GetType().GetProperties();
  172. }
  173. public void M_Set_InstancPropertiValue(object obj_Instanc,string str_PropertieName,object values)
  174. {
  175. PropertyInfo property = obj_Instanc.GetType().GetProperty(str_PropertieName);
  176. property.SetValue(obj_Instanc,values,null);
  177. }
  178. public object M_Get_InstancPropertiValue(object obj_Instanc,string str_PropertieName)
  179. {
  180. if (obj_Instanc == null)
  181. {
  182. return null;
  183. }
  184. PropertyInfo property = obj_Instanc.GetType().GetProperty(str_PropertieName);
  185. if (property != null)
  186. {
  187. return property.GetValue(obj_Instanc,null);
  188. }
  189. return null;
  190. }
  191. public bool M_SetEventPropressFun(object obj_targetInstance,string eventname,object obj_processInstance,string funname)
  192. {
  193. bool flag = false;
  194. try
  195. {
  196. TypeHelper m_TypeHelper = new TypeHelper();
  197. obj_targetInstance.GetType();
  198. EventInfo @event = obj_targetInstance.GetType().GetEvent(eventname);
  199. Type eventHandlerType = @event.EventHandlerType;
  200. MethodInfo m_MethodInfo = m_TypeHelper.M_GetMethodInfosByName(obj_processInstance.GetType(),funname)[0].M_MethodInfo;
  201. Delegate handler = Delegate.CreateDelegate(eventHandlerType,obj_processInstance,funname);
  202. @event.AddEventHandler(obj_targetInstance,handler);
  203. return true;
  204. }
  205. catch (Exception)
  206. {
  207. flag = false;
  208. throw;
  209. }
  210. }
  211. }
  212. }