123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace ProjectBase.Controls.Others
- {
- internal sealed class ReflectionUtil
- {
- private ReflectionUtil()
- {
- }
- /// <summary>
- /// 绑定标识
- /// </summary>
- public static BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public |
- BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
- /// <summary>
- /// 执行方法
- /// </summary>
- /// <param name="obj">对象实例</param>
- /// <param name="methodName">方法名称</param>
- /// <param name="args">参数</param>
- /// <returns></returns>
- public static object InvokeMethod(object obj, string methodName, object[] args)
- {
- object objReturn = null;
- Type type = obj.GetType();
- objReturn = type.InvokeMember(methodName, bf | BindingFlags.InvokeMethod, null, obj, args);
- return objReturn;
- }
- /// <summary>
- /// 设置对象实例的字段值
- /// </summary>
- /// <param name="obj">对象实例</param>
- /// <param name="name">字段名称</param>
- /// <param name="value">字段值</param>
- public static void SetField(object obj, string name, object value)
- {
- FieldInfo fi = obj.GetType().GetField(name, bf);
- fi.SetValue(obj, value);
- }
- /// <summary>
- /// 获取对象实例的字段值
- /// </summary>
- /// <param name="obj">对象实例</param>
- /// <param name="name">字段名称</param>
- /// <returns></returns>
- public static object GetField(object obj, string name)
- {
- FieldInfo fi = obj.GetType().GetField(name, bf);
- return fi.GetValue(obj);
- }
- /// <summary>
- /// 设置对象属性的值
- /// </summary>
- /// <param name="obj">对象实例</param>
- /// <param name="name">属性名称</param>
- /// <param name="value">属性值</param>
- public static void SetProperty(object obj, string name, object value)
- {
- PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bf);
- object objValue = Convert.ChangeType(value, propertyInfo.PropertyType);
- propertyInfo.SetValue(obj, objValue, null);
- }
- /// <summary>
- /// 获取对象属性的值
- /// </summary>
- public static object GetProperty(object obj, string name)
- {
- PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bf);
- return propertyInfo.GetValue(obj, null);
- }
- /// <summary>
- /// 获取对象属性信息(组装成字符串输出)
- /// </summary>
- public static List<string> GetPropertyNames(object obj)
- {
- List<string> nameList = new List<string>();
- PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
- foreach (PropertyInfo property in propertyInfos)
- {
- nameList.Add(property.Name);
- }
- return nameList;
- }
- /// <summary>
- /// 获取对象属性信息(组装成字符串输出)
- /// </summary>
- public static Dictionary<string, string> GetPropertyNameTypes(object obj)
- {
- Dictionary<string, string> nameList = new Dictionary<string, string>();
- PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
- foreach (PropertyInfo property in propertyInfos)
- {
- nameList.Add(property.Name, property.PropertyType.FullName);
- }
- return nameList;
- }
- public static DataTable CreateTable(object objSource)
- {
- DataTable table = null;
- IEnumerable objList = objSource as IEnumerable;
- if (objList == null)
- return null;
- foreach (object obj in objList)
- {
- if (table == null)
- {
- List<string> nameList = ReflectionUtil.GetPropertyNames(obj);
- table = new DataTable("");
- DataColumn column;
- foreach (string name in nameList)
- {
- column = new DataColumn();
- column.DataType = System.Type.GetType("System.String");
- column.ColumnName = name;
- column.Caption = name;
- table.Columns.Add(column);
- }
- }
- DataRow row = table.NewRow();
- PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
- foreach (PropertyInfo property in propertyInfos)
- {
- row[property.Name] = property.GetValue(obj, null);
- }
- table.Rows.Add(row);
- }
- return table;
- }
- /// <summary>
- /// 创建对应实例
- /// </summary>
- /// <param name="type">类型</param>
- /// <returns>对应实例</returns>
- public static object CreateInstance(string type)
- {
- Type tmp = null;
- Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
- for (int i = 0; i < assemblies.Length; i++)
- {
- tmp = assemblies[i].GetType(type);
- if (tmp != null)
- {
- return assemblies[i].CreateInstance(type);
- }
- }
- return null;
- //return Assembly.GetExecutingAssembly().CreateInstance(type);
- }
- /// <summary>
- /// 创建对应实例
- /// </summary>
- /// <param name="type">类型</param>
- /// <returns>对应实例</returns>
- public static object CreateInstance(Type type)
- {
- return CreateInstance(type.FullName);
- }
- }
- }
|