PropertyUtil.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Reflection;
  5. using System.Collections;
  6. using CommonLang.Log;
  7. namespace CommonLang.Property
  8. {
  9. public static class PropertyUtil
  10. {
  11. private static Logger s_log;
  12. public static Logger log { get { if (s_log == null) { s_log = LoggerFactory.GetLogger(typeof(PropertyUtil).Name); } return s_log; } }
  13. static public FieldInfo[] SortFields(FieldInfo[] fields)
  14. {
  15. Array.Sort(fields, BaseFieldSorter);
  16. return fields;
  17. }
  18. static public PropertyInfo[] SortProperties(PropertyInfo[] properties)
  19. {
  20. Array.Sort(properties, BasePropertySorter);
  21. return properties;
  22. }
  23. static public IComparer<FieldInfo> BaseFieldSorter = new FieldSorter();
  24. static public IComparer<PropertyInfo> BasePropertySorter = new PropertySorter();
  25. public class FieldSorter : IComparer<FieldInfo>
  26. {
  27. public int Compare(FieldInfo x, FieldInfo y)
  28. {
  29. return x.Name.CompareTo(y.Name);
  30. }
  31. }
  32. public class PropertySorter : IComparer<PropertyInfo>
  33. {
  34. public int Compare(PropertyInfo x, PropertyInfo y)
  35. {
  36. return x.Name.CompareTo(y.Name);
  37. }
  38. }
  39. public static Attribute GetAttributeByType(FieldInfo field, Type attributeType)
  40. {
  41. object[] cas = field.GetCustomAttributes(attributeType, false);
  42. if (cas != null && cas.Length > 0)
  43. {
  44. return cas[0] as Attribute;
  45. }
  46. return null;
  47. }
  48. public static T GetAttribute<T>(Type type) where T : System.Attribute
  49. {
  50. while (!type.Equals(typeof(object)))
  51. {
  52. object[] cas = type.GetCustomAttributes(typeof(T), false);
  53. if (cas != null && cas.Length > 0)
  54. {
  55. return (T)cas[0];
  56. }
  57. else if (type.BaseType != null)
  58. {
  59. type = type.BaseType;
  60. }
  61. else { return null; }
  62. }
  63. return null;
  64. }
  65. public static T GetAttribute<T>(MemberInfo member) where T : System.Attribute
  66. {
  67. object[] cas = member.GetCustomAttributes(typeof(T), false);
  68. if (cas != null && cas.Length > 0)
  69. {
  70. return (T)cas[0];
  71. }
  72. return null;
  73. }
  74. public static T GetAttribute<T>(FieldInfo field) where T : System.Attribute
  75. {
  76. object[] cas = field.GetCustomAttributes(typeof(T), false);
  77. if (cas != null && cas.Length > 0)
  78. {
  79. return (T)cas[0];
  80. }
  81. return null;
  82. }
  83. public static T GetAttribute<T>(PropertyInfo property) where T : System.Attribute
  84. {
  85. object[] cas = property.GetCustomAttributes(typeof(T), false);
  86. if (cas != null && cas.Length > 0)
  87. {
  88. return (T)cas[0];
  89. }
  90. return null;
  91. }
  92. static public T GetEnumAttribute<T>(object value) where T : Attribute
  93. {
  94. Type type = value.GetType();
  95. string name = Enum.GetName(type, value);
  96. if (name == null)
  97. {
  98. return null;
  99. }
  100. FieldInfo field = type.GetField(name);
  101. if (field != null)
  102. {
  103. return GetAttribute<T>(field);
  104. }
  105. return null;
  106. }
  107. public static DescAttribute GetDesc(Type type)
  108. {
  109. return GetAttribute<DescAttribute>(type);
  110. }
  111. public static DescAttribute GetDesc(MemberInfo field)
  112. {
  113. return GetAttribute<DescAttribute>(field);
  114. }
  115. public static ListAttribute GetListDesc(MemberInfo field)
  116. {
  117. return GetAttribute<ListAttribute>(field);
  118. }
  119. /// <summary>
  120. /// 将一个对象里面所有的Attribute的标记的Field值,全部取出
  121. /// </summary>
  122. /// <typeparam name="T"></typeparam>
  123. /// <param name="data"></param>
  124. /// <param name="collection"></param>
  125. /// <returns></returns>
  126. static public void CollectFieldTypeValues<T>(object data, List<T> collection)
  127. {
  128. if (data != null)
  129. {
  130. Type fieldType = typeof(T);
  131. Type type = data.GetType();
  132. if (data is T)
  133. {
  134. collection.Add((T)data);
  135. }
  136. if (type.IsClass && !type.IsPrimitive)
  137. {
  138. if (data is IDictionary)
  139. {
  140. IDictionary map = data as IDictionary;
  141. foreach (object o in map.Values)
  142. {
  143. CollectFieldTypeValues<T>(o, collection);
  144. }
  145. }
  146. else if (data is ICollection)
  147. {
  148. ICollection list = data as ICollection;
  149. foreach (object o in list)
  150. {
  151. CollectFieldTypeValues<T>(o, collection);
  152. }
  153. }
  154. else if (type.IsArray)
  155. {
  156. Array array = (Array)data;
  157. foreach (object o in array)
  158. {
  159. CollectFieldTypeValues<T>(o, collection);
  160. }
  161. }
  162. else
  163. {
  164. foreach (FieldInfo field in type.GetFields())
  165. {
  166. if (!field.IsStatic)
  167. {
  168. object fd = field.GetValue(data);
  169. if (fd != null)
  170. {
  171. CollectFieldTypeValues<T>(fd, collection);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. /// <summary>
  180. /// 将一个对象里面所有的Attribute的标记的Field值,全部取出
  181. /// </summary>
  182. /// <param name="data"></param>
  183. /// <param name="attributeType"></param>
  184. /// <param name="collection"></param>
  185. static public void CollectFieldAttributeValues(object data, Type attributeType, List<FieldAttributeValue> collection)
  186. {
  187. if (data != null)
  188. {
  189. Type type = data.GetType();
  190. if (data is IDictionary)
  191. {
  192. IDictionary map = data as IDictionary;
  193. foreach (object o in map.Values)
  194. {
  195. CollectFieldAttributeValues(o, attributeType, collection);
  196. }
  197. }
  198. else if (data is ICollection)
  199. {
  200. ICollection list = data as ICollection;
  201. foreach (object o in list)
  202. {
  203. CollectFieldAttributeValues(o, attributeType, collection);
  204. }
  205. }
  206. else if (type.IsArray)
  207. {
  208. Array array = (Array)data;
  209. foreach (object o in array)
  210. {
  211. CollectFieldAttributeValues(o, attributeType, collection);
  212. }
  213. }
  214. else if (type.IsClass)
  215. {
  216. foreach (FieldInfo field in type.GetFields())
  217. {
  218. if (!field.IsStatic)
  219. {
  220. object fd = field.GetValue(data);
  221. if (fd != null)
  222. {
  223. Attribute attr = PropertyUtil.GetAttributeByType(field, attributeType);
  224. if (attr != null)
  225. {
  226. FieldAttributeValue fv = new FieldAttributeValue(field, attr, fd, data);
  227. collection.Add(fv);
  228. }
  229. else
  230. {
  231. CollectFieldAttributeValues(fd, attributeType, collection);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. static public void CollectFieldAttributeValuesGeneric<A, T>(object data, List<TFieldAttributeValue<A, T>> collection) where A : Attribute
  240. {
  241. List<FieldAttributeValue> list = new List<FieldAttributeValue>();
  242. CollectFieldAttributeValues(data, typeof(A), list);
  243. foreach (FieldAttributeValue fv in list)
  244. {
  245. collection.Add(new TFieldAttributeValue<A, T>(fv.Field, fv.AttributeData as A, fv.FieldValue, fv.FieldOwner));
  246. }
  247. }
  248. /// <summary>
  249. /// 返回书写习惯的类型字符串
  250. /// </summary>
  251. /// <param name="type"></param>
  252. /// <returns></returns>
  253. public static string ToTypeDefineString(Type type)
  254. {
  255. if (type.IsGenericType)
  256. {
  257. StringBuilder sb = new StringBuilder();
  258. int tidx = type.Name.IndexOf('`');
  259. if (tidx > 0)
  260. {
  261. sb.Append(type.Name.Substring(0, tidx));
  262. }
  263. else
  264. {
  265. sb.Append(type.Name);
  266. }
  267. sb.Append("<");
  268. Type[] g_args = type.GetGenericArguments();
  269. foreach (Type g_arg in g_args)
  270. {
  271. sb.Append(ToTypeDefineString(g_arg));
  272. if (!g_arg.Equals(g_args[g_args.Length - 1]))
  273. {
  274. sb.Append(", ");
  275. }
  276. }
  277. sb.Append(">");
  278. return sb.ToString();
  279. }
  280. return type.Name;
  281. }
  282. public static T GetFieldOrPropertyOrMethodValue<T>(object componentData, string fieldName)
  283. {
  284. Type componentType = componentData.GetType();
  285. FieldInfo depend_field = componentType.GetField(fieldName);
  286. if (depend_field != null)
  287. {
  288. try
  289. {
  290. T ret = (T)depend_field.GetValue(componentData);
  291. return ret;
  292. }
  293. catch (Exception err) { log.Error(err.Message, err); }
  294. }
  295. PropertyInfo depend_property = componentType.GetProperty(fieldName);
  296. if (depend_property != null)
  297. {
  298. try
  299. {
  300. T ret = (T)depend_property.GetValue(componentData, null);
  301. return ret;
  302. }
  303. catch (Exception err) { log.Error(err.Message, err); }
  304. }
  305. MethodInfo depend_method = componentType.GetMethod(fieldName);
  306. if (depend_method != null)
  307. {
  308. try
  309. {
  310. T ret = (T)depend_method.Invoke(componentData, ZERO_ARGS);
  311. return ret;
  312. }
  313. catch (Exception err) { log.Error(err.Message, err); }
  314. }
  315. return default(T);
  316. }
  317. public readonly static object[] ZERO_ARGS = new object[] { };
  318. public static void SetMemberValue(MemberInfo field, object obj, object value)
  319. {
  320. if (field is FieldInfo)
  321. {
  322. (field as FieldInfo).SetValue(obj, value);
  323. }
  324. else if (field is PropertyInfo)
  325. {
  326. var set = (field as PropertyInfo).GetSetMethod();
  327. if (set != null)
  328. {
  329. set.Invoke(obj, new object[] { value });
  330. }
  331. }
  332. }
  333. public static object GetMemberValue(MemberInfo field, object obj)
  334. {
  335. if (field is FieldInfo)
  336. {
  337. return (field as FieldInfo).GetValue(obj);
  338. }
  339. else if (field is PropertyInfo)
  340. {
  341. var get = (field as PropertyInfo).GetGetMethod();
  342. if (get != null)
  343. {
  344. return get.Invoke(obj, ZERO_ARGS);
  345. }
  346. }
  347. return null;
  348. }
  349. }
  350. public struct FieldAttributeValue
  351. {
  352. public Attribute AttributeData;
  353. public FieldInfo Field;
  354. public object FieldOwner;
  355. public object FieldValue;
  356. public FieldAttributeValue(FieldInfo field, Attribute attr, object fieldData, object fieldOwner)
  357. {
  358. this.AttributeData = attr;
  359. this.Field = field;
  360. this.FieldOwner = fieldOwner;
  361. this.FieldValue = fieldData;
  362. }
  363. }
  364. public struct TFieldAttributeValue<A, T> where A : Attribute
  365. {
  366. public A AttributeData;
  367. public FieldInfo Field;
  368. public object FieldOwner;
  369. public T FieldValue;
  370. public TFieldAttributeValue(FieldInfo field, A attr, object fieldData, object fieldOwner)
  371. {
  372. this.AttributeData = attr;
  373. this.Field = field;
  374. this.FieldOwner = fieldOwner;
  375. this.FieldValue = (T)fieldData;
  376. }
  377. }
  378. public class TypeDescAttribute : IComparable<TypeDescAttribute>
  379. {
  380. public DescAttribute Desc;
  381. public Type DataType;
  382. public TypeDescAttribute(Type type)
  383. {
  384. DataType = type;
  385. Desc = PropertyUtil.GetAttribute<DescAttribute>(type);
  386. }
  387. public override string ToString()
  388. {
  389. return Desc.Desc;
  390. }
  391. public int CompareTo(TypeDescAttribute other)
  392. {
  393. return this.ToString().CompareTo(other.ToString());
  394. }
  395. /// <summary>
  396. /// 如果此类型有DescAttribute签名,则返回Desc
  397. /// </summary>
  398. /// <param name="type"></param>
  399. /// <returns></returns>
  400. public static string GetDescText(Type type)
  401. {
  402. DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(type);
  403. if (desc != null)
  404. {
  405. return desc.Desc;
  406. }
  407. return "";
  408. }
  409. /// <summary>
  410. /// 如果此类型有DescAttribute签名,则返回Catgory
  411. /// </summary>
  412. /// <param name="type"></param>
  413. /// <returns></returns>
  414. public static string GetCatgoryName(Type type)
  415. {
  416. DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(type);
  417. if (desc != null)
  418. {
  419. return desc.Category;
  420. }
  421. return "";
  422. }
  423. }
  424. public class MemberDescAttribute : IComparable<MemberDescAttribute>
  425. {
  426. public readonly DescAttribute Desc;
  427. public readonly MemberInfo DataField;
  428. public readonly Type FieldType;
  429. public MemberDescAttribute(MemberInfo field)
  430. {
  431. DataField = field;
  432. Desc = PropertyUtil.GetAttribute<DescAttribute>(field);
  433. if (field is FieldInfo)
  434. {
  435. FieldType = (field as FieldInfo).FieldType;
  436. }
  437. else if (field is PropertyInfo)
  438. {
  439. FieldType = (field as PropertyInfo).PropertyType;
  440. }
  441. else if (field is MethodInfo)
  442. {
  443. FieldType = (field as MethodInfo).ReturnType;
  444. }
  445. else
  446. {
  447. throw new Exception("Error MemberDescAttribute : " + field);
  448. }
  449. }
  450. public string ToDescString()
  451. {
  452. return (Desc != null ? string.Format("[{0}]", Desc.Desc) : "");
  453. }
  454. public override string ToString()
  455. {
  456. if (DataField is MethodInfo)
  457. {
  458. return string.Format("{0}() {1}", DataField.Name, ToDescString());
  459. }
  460. else
  461. {
  462. return string.Format("{0} {1}", DataField.Name, ToDescString());
  463. }
  464. }
  465. public int CompareTo(MemberDescAttribute other)
  466. {
  467. return DataField.Name.CompareTo(other.DataField.Name);
  468. }
  469. public object GetValue(object owner)
  470. {
  471. try
  472. {
  473. if (DataField is FieldInfo)
  474. {
  475. FieldInfo field = DataField as FieldInfo;
  476. return field.GetValue(owner);
  477. }
  478. else if (DataField is PropertyInfo)
  479. {
  480. PropertyInfo field = DataField as PropertyInfo;
  481. return field.GetGetMethod().Invoke(owner, new object[0]);
  482. }
  483. else if (DataField is MethodInfo)
  484. {
  485. MethodInfo method = DataField as MethodInfo;
  486. return method.Invoke(owner, new object[0]);
  487. }
  488. }
  489. catch (Exception err) { PropertyUtil.log.Error(err.Message, err); }
  490. return null;
  491. }
  492. public void SetValue(object owner, object value)
  493. {
  494. try
  495. {
  496. if (DataField is FieldInfo)
  497. {
  498. FieldInfo field = DataField as FieldInfo;
  499. field.SetValue(owner, value);
  500. }
  501. else if (DataField is PropertyInfo)
  502. {
  503. PropertyInfo field = DataField as PropertyInfo;
  504. field.GetSetMethod().Invoke(owner, new object[] { value });
  505. }
  506. else if (DataField is MethodInfo)
  507. {
  508. MethodInfo method = DataField as MethodInfo;
  509. method.Invoke(owner, new object[] { value });
  510. }
  511. }
  512. catch (Exception err) { PropertyUtil.log.Error(err.Message, err); }
  513. }
  514. }
  515. }