Catgorys.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CommonLang.Property;
  6. using CommonLang;
  7. namespace CommonLang.Property
  8. {
  9. public class Catgorys
  10. {
  11. private List<Type> triggerTypes;
  12. private HashMap<string, List<TypeDescAttribute>> catgorysMap = new HashMap<string, List<TypeDescAttribute>>();
  13. private List<string> catgorysName = new List<string>();
  14. public Catgorys(Type baseType)
  15. {
  16. BaseType = baseType;
  17. triggerTypes = ReflectionUtil.GetNoneVirtualSubTypes(baseType);
  18. foreach (Type type in triggerTypes)
  19. {
  20. try
  21. {
  22. TypeDescAttribute tt = new TypeDescAttribute(type);
  23. List<TypeDescAttribute> catgorys = catgorysMap.Get(tt.Desc.Category);
  24. if (catgorys == null)
  25. {
  26. catgorys = new List<TypeDescAttribute>();
  27. catgorysName.Add(tt.Desc.Category);
  28. catgorysMap.Put(tt.Desc.Category, catgorys);
  29. }
  30. catgorys.Add(tt);
  31. }
  32. catch (Exception err)
  33. {
  34. Console.WriteLine("Catgorys catch: " + err);
  35. }
  36. }
  37. }
  38. public Type BaseType { get; private set; }
  39. public List<string> CatgoryNames { get { return catgorysName; } }
  40. public List<TypeDescAttribute> GetCatgoryTypes(string name)
  41. {
  42. return catgorysMap.Get(name);
  43. }
  44. /// <summary>
  45. /// 找到Type对应的TypeDescAttribute
  46. /// </summary>
  47. /// <param name="type"></param>
  48. /// <returns></returns>
  49. public TypeDescAttribute GetTypeDescAttribute(Type type)
  50. {
  51. DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(type);
  52. if (desc != null)
  53. {
  54. List<TypeDescAttribute> catgorys = catgorysMap.Get(desc.Category);
  55. if (catgorys != null)
  56. {
  57. foreach (TypeDescAttribute ta in catgorys)
  58. {
  59. if (ta.DataType == type)
  60. {
  61. return ta;
  62. }
  63. }
  64. }
  65. }
  66. return null;
  67. }
  68. private static HashMap<Type, Catgorys> CatgorysMap = new HashMap<Type, Catgorys>();
  69. public static Catgorys GetCatgory(Type baseType)
  70. {
  71. Catgorys ret = CatgorysMap.Get(baseType);
  72. if (ret == null)
  73. {
  74. ret = new Catgorys(baseType);
  75. CatgorysMap.Put(baseType, ret);
  76. }
  77. return ret;
  78. }
  79. }
  80. }