ReflectionUtil.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ProjectBase.Controls.Others
  10. {
  11. internal sealed class ReflectionUtil
  12. {
  13. private ReflectionUtil()
  14. {
  15. }
  16. /// <summary>
  17. /// 绑定标识
  18. /// </summary>
  19. public static BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public |
  20. BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
  21. /// <summary>
  22. /// 执行方法
  23. /// </summary>
  24. /// <param name="obj">对象实例</param>
  25. /// <param name="methodName">方法名称</param>
  26. /// <param name="args">参数</param>
  27. /// <returns></returns>
  28. public static object InvokeMethod(object obj, string methodName, object[] args)
  29. {
  30. object objReturn = null;
  31. Type type = obj.GetType();
  32. objReturn = type.InvokeMember(methodName, bf | BindingFlags.InvokeMethod, null, obj, args);
  33. return objReturn;
  34. }
  35. /// <summary>
  36. /// 设置对象实例的字段值
  37. /// </summary>
  38. /// <param name="obj">对象实例</param>
  39. /// <param name="name">字段名称</param>
  40. /// <param name="value">字段值</param>
  41. public static void SetField(object obj, string name, object value)
  42. {
  43. FieldInfo fi = obj.GetType().GetField(name, bf);
  44. fi.SetValue(obj, value);
  45. }
  46. /// <summary>
  47. /// 获取对象实例的字段值
  48. /// </summary>
  49. /// <param name="obj">对象实例</param>
  50. /// <param name="name">字段名称</param>
  51. /// <returns></returns>
  52. public static object GetField(object obj, string name)
  53. {
  54. FieldInfo fi = obj.GetType().GetField(name, bf);
  55. return fi.GetValue(obj);
  56. }
  57. /// <summary>
  58. /// 设置对象属性的值
  59. /// </summary>
  60. /// <param name="obj">对象实例</param>
  61. /// <param name="name">属性名称</param>
  62. /// <param name="value">属性值</param>
  63. public static void SetProperty(object obj, string name, object value)
  64. {
  65. PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bf);
  66. object objValue = Convert.ChangeType(value, propertyInfo.PropertyType);
  67. propertyInfo.SetValue(obj, objValue, null);
  68. }
  69. /// <summary>
  70. /// 获取对象属性的值
  71. /// </summary>
  72. public static object GetProperty(object obj, string name)
  73. {
  74. PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bf);
  75. return propertyInfo.GetValue(obj, null);
  76. }
  77. /// <summary>
  78. /// 获取对象属性信息(组装成字符串输出)
  79. /// </summary>
  80. public static List<string> GetPropertyNames(object obj)
  81. {
  82. List<string> nameList = new List<string>();
  83. PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
  84. foreach (PropertyInfo property in propertyInfos)
  85. {
  86. nameList.Add(property.Name);
  87. }
  88. return nameList;
  89. }
  90. /// <summary>
  91. /// 获取对象属性信息(组装成字符串输出)
  92. /// </summary>
  93. public static Dictionary<string, string> GetPropertyNameTypes(object obj)
  94. {
  95. Dictionary<string, string> nameList = new Dictionary<string, string>();
  96. PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
  97. foreach (PropertyInfo property in propertyInfos)
  98. {
  99. nameList.Add(property.Name, property.PropertyType.FullName);
  100. }
  101. return nameList;
  102. }
  103. public static DataTable CreateTable(object objSource)
  104. {
  105. DataTable table = null;
  106. IEnumerable objList = objSource as IEnumerable;
  107. if (objList == null)
  108. return null;
  109. foreach (object obj in objList)
  110. {
  111. if (table == null)
  112. {
  113. List<string> nameList = ReflectionUtil.GetPropertyNames(obj);
  114. table = new DataTable("");
  115. DataColumn column;
  116. foreach (string name in nameList)
  117. {
  118. column = new DataColumn();
  119. column.DataType = System.Type.GetType("System.String");
  120. column.ColumnName = name;
  121. column.Caption = name;
  122. table.Columns.Add(column);
  123. }
  124. }
  125. DataRow row = table.NewRow();
  126. PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf);
  127. foreach (PropertyInfo property in propertyInfos)
  128. {
  129. row[property.Name] = property.GetValue(obj, null);
  130. }
  131. table.Rows.Add(row);
  132. }
  133. return table;
  134. }
  135. /// <summary>
  136. /// 创建对应实例
  137. /// </summary>
  138. /// <param name="type">类型</param>
  139. /// <returns>对应实例</returns>
  140. public static object CreateInstance(string type)
  141. {
  142. Type tmp = null;
  143. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  144. for (int i = 0; i < assemblies.Length; i++)
  145. {
  146. tmp = assemblies[i].GetType(type);
  147. if (tmp != null)
  148. {
  149. return assemblies[i].CreateInstance(type);
  150. }
  151. }
  152. return null;
  153. //return Assembly.GetExecutingAssembly().CreateInstance(type);
  154. }
  155. /// <summary>
  156. /// 创建对应实例
  157. /// </summary>
  158. /// <param name="type">类型</param>
  159. /// <returns>对应实例</returns>
  160. public static object CreateInstance(Type type)
  161. {
  162. return CreateInstance(type.FullName);
  163. }
  164. }
  165. }