using BPASmartClient.MessageCommunication.MsgControl.Model; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.MessageCommunication.MsgControl { public class TypeHelper { public Type M_GetTypeByName(string filepath,string TypeName) { try { if (!File.Exists(filepath)) { return null; } Assembly assembly = Assembly.LoadFrom(filepath); Type[] types = assembly.GetTypes(); foreach (Type type in types) { if (type.Name == TypeName) { return type; } } } catch (Exception ex) { Exception ex2 = ex; return null; } return null; } public Type M_GetTypeByFullName(string filepath,string TypeFullName) { try { if (!File.Exists(filepath)) { return null; } Assembly assembly = Assembly.LoadFrom(filepath); Type[] types = assembly.GetTypes(); foreach (Type type in types) { if (type.FullName == TypeFullName) { return type; } } } catch (Exception ex) { Exception ex2 = ex; return null; } return null; } public object M_GetPropertyValue(object obj,string Str_PropertyName) { Type type = obj.GetType(); PropertyInfo property = type.GetProperty(Str_PropertyName); if (null != property) { return property.GetValue(obj,null); } return null; } public void M_SetFieldInfoValue(object Target_obj,string Str_PropertyName,object obj_Value) { Type type = Target_obj.GetType(); FieldInfo field = type.GetField(Str_PropertyName); field.SetValue(Target_obj,obj_Value); } public FieldInfo[] M_Get_PublicFieldInfos(Type type) { return type.GetFields(); } public FieldInfo[] M_Get_AllFieldInfosByObject(object obj) { Type type = obj.GetType(); return type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); } public FieldInfo[] M_Get_AllFieldInfos(Type type) { return type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); } public Type M_GetPropertyType(object Target_obj,string Str_PropertyName) { Type type = Target_obj.GetType(); PropertyInfo property = type.GetProperty(Str_PropertyName); return property.PropertyType; } public MethodInfo[] M_GetMethodInfos(Type userType) { return userType.GetMethods(); } public List<方法描述> M_GetMethodInfosByName(Type userType,string str_MethedName) { List<方法描述> list = new List<方法描述>(); MethodInfo[] methods = userType.GetMethods(); foreach (MethodInfo methodInfo in methods) { if (methodInfo.Name == str_MethedName) { 方法描述 方法描述 = new 方法描述(); 方法描述.Obj_Type = userType; 方法描述.M_MethodInfo = methodInfo; 方法描述.M_ParameterInfos = methodInfo.GetParameters(); list.Add(方法描述); } } return list; } public 方法描述 M_GetMethodInfosByNameandParmarTypes(Type userType,string str_MethedName,Type[] parmerTypes) { //IL_002b: Unknown result type (might be due to invalid IL or missing references) MethodInfo method; try { method = userType.GetMethod(str_MethedName,parmerTypes); } catch (Exception ex) { //MessageBox.Show("对" + userType.FullName + "执行: M_GetMethodInfosByNameandParmarTypes发生异常," + ex.Message); return null; } if (method == null) { return null; } 方法描述 方法描述 = new 方法描述(); 方法描述.Obj_Type = userType; 方法描述.M_MethodInfo = method; 方法描述.M_ParameterInfos = method.GetParameters(); return 方法描述; } public object M_Call_InstancMethod(object objInstance,string str_MethodName,object[] obj_Parmeters) { MethodInfo method; if (obj_Parmeters != null) { Type[] array = new Type[obj_Parmeters.Count()]; for (int i = 0; i < obj_Parmeters.Count(); i++) { array[i] = obj_Parmeters[i].GetType(); } method = objInstance.GetType().GetMethod(str_MethodName,array); return method.Invoke(objInstance,obj_Parmeters); } method = objInstance.GetType().GetMethod(str_MethodName,new Type[0]); return method.Invoke(objInstance,null); } public EventInfo[] M_GetEvents(Type userType) { return userType.GetEvents(); } public void M_AddEventProcess() { } public PropertyInfo[] M_GetPropertyInfos(Type userType) { return userType.GetProperties(); } public PropertyInfo[] M_GetPropertyInfos(object userObject) { return userObject.GetType().GetProperties(); } public void M_Set_InstancPropertiValue(object obj_Instanc,string str_PropertieName,object values) { PropertyInfo property = obj_Instanc.GetType().GetProperty(str_PropertieName); property.SetValue(obj_Instanc,values,null); } public object M_Get_InstancPropertiValue(object obj_Instanc,string str_PropertieName) { if (obj_Instanc == null) { return null; } PropertyInfo property = obj_Instanc.GetType().GetProperty(str_PropertieName); if (property != null) { return property.GetValue(obj_Instanc,null); } return null; } public bool M_SetEventPropressFun(object obj_targetInstance,string eventname,object obj_processInstance,string funname) { bool flag = false; try { TypeHelper m_TypeHelper = new TypeHelper(); obj_targetInstance.GetType(); EventInfo @event = obj_targetInstance.GetType().GetEvent(eventname); Type eventHandlerType = @event.EventHandlerType; MethodInfo m_MethodInfo = m_TypeHelper.M_GetMethodInfosByName(obj_processInstance.GetType(),funname)[0].M_MethodInfo; Delegate handler = Delegate.CreateDelegate(eventHandlerType,obj_processInstance,funname); @event.AddEventHandler(obj_targetInstance,handler); return true; } catch (Exception) { flag = false; throw; } } } }