版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

253 regels
6.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.ComponentModel;
  7. using System.Collections;
  8. namespace ProductionControl.UI.PropExtend
  9. {
  10. [AttributeUsage(AttributeTargets.Property)]
  11. public class PropertyOrderAttribute : Attribute//自定义Attribute类,向property提供
  12. {
  13. private int order;
  14. public PropertyOrderAttribute(int order)
  15. {
  16. this.order = order;
  17. }
  18. public int Order
  19. {
  20. get
  21. {
  22. return order;
  23. }
  24. }
  25. }
  26. #region Helper Class - PropertyOrderPair
  27. public class PropertyOrderPair : IComparable
  28. {
  29. private int _order;
  30. private string _name;
  31. public string Name
  32. {
  33. get
  34. {
  35. return _name;
  36. }
  37. }
  38. public PropertyOrderPair(string name, int order)
  39. {
  40. _order = order;
  41. _name = name;
  42. }
  43. public int CompareTo(object obj)
  44. {
  45. //
  46. // Sort the pair objects by ordering by order value
  47. // Equal values get the same rank
  48. //
  49. int otherOrder = ((PropertyOrderPair)obj)._order;
  50. if (otherOrder == _order)
  51. {
  52. //
  53. // If order not specified, sort by name
  54. //
  55. string otherName = ((PropertyOrderPair)obj)._name;
  56. return string.Compare(_name, otherName);
  57. }
  58. else if (otherOrder > _order)
  59. {
  60. return -1;
  61. }
  62. return 1;
  63. }
  64. }
  65. #endregion
  66. class TestPropertyDescriptor : PropertyDescriptor, IComparable//继承PropertyDescriptor类并实现IComparable接口
  67. {
  68. private PropertyDescriptor basePropertyDescriptor;
  69. private int order;
  70. //构造函数
  71. public TestPropertyDescriptor(PropertyDescriptor basePropertyDescriptor) : base(basePropertyDescriptor)
  72. {
  73. this.basePropertyDescriptor = basePropertyDescriptor;
  74. order = GetOrder(basePropertyDescriptor.Attributes);
  75. }
  76. //获得property的order属性
  77. private int GetOrder(AttributeCollection ac)
  78. {
  79. foreach (Attribute a in ac)
  80. {
  81. if (a is PropertyOrderAttribute)
  82. return ((PropertyOrderAttribute)a).Order;
  83. }
  84. return 0;
  85. }
  86. #region "IComparable"
  87. public int CompareTo(object tpd)//实现接口,使此类的对象可以依据order进行比较、排序
  88. {
  89. TestPropertyDescriptor other = (TestPropertyDescriptor)tpd;
  90. if (order == other.order) return string.Compare(Name, other.Name);
  91. else return (order > other.order) ? 1 : -1;
  92. }
  93. public override bool CanResetValue(object component)
  94. {
  95. return false;
  96. }
  97. public override Type ComponentType
  98. {
  99. get
  100. {
  101. return this.GetType();
  102. }
  103. }
  104. public override object GetValue(object component)
  105. {
  106. return component;
  107. }
  108. public override bool IsReadOnly
  109. {
  110. get
  111. {
  112. return false;
  113. }
  114. }
  115. public override Type PropertyType
  116. {
  117. get
  118. {
  119. return this.GetType();
  120. }
  121. }
  122. public override void ResetValue(object component)
  123. {
  124. //不重置,无动作
  125. }
  126. public override void SetValue(object component, object value)
  127. {
  128. ;
  129. }
  130. /// <summary>
  131. /// 是否应该持久化保存
  132. /// </summary>
  133. /// <param name="component"></param>
  134. /// <returns></returns>
  135. public override bool ShouldSerializeValue(object component)
  136. {
  137. return false;
  138. }
  139. /// <summary>
  140. /// 属性说明
  141. /// </summary>
  142. public override string Description
  143. {
  144. get
  145. {
  146. return this.Description;
  147. }
  148. }
  149. #endregion
  150. }
  151. class ICustomTDClass1 : ICustomTypeDescriptor//Class1为需要对其属性进行排序的自定义类。
  152. {
  153. public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  154. {
  155. PropertyDescriptorCollection tmpPDC = TypeDescriptor.GetProperties(typeof(ICustomTDClass1), attributes);
  156. PropertyDescriptorCollection result = new PropertyDescriptorCollection(null);
  157. ArrayList orderPdList = new ArrayList();
  158. foreach (PropertyDescriptor pd in tmpPDC)
  159. {
  160. TestPropertyDescriptor tpd = new TestPropertyDescriptor(pd);
  161. result.Add(tpd);
  162. orderPdList.Add(tpd);
  163. }
  164. orderPdList.Sort();//根据order排序
  165. ArrayList propertyNames = new ArrayList();
  166. foreach (TestPropertyDescriptor propertyAttributes in orderPdList)//获得排序后的DisplayName数组
  167. {
  168. propertyNames.Add(propertyAttributes.DisplayName);
  169. }
  170. return result.Sort((string[])propertyNames.ToArray(typeof(string)));//根据数组对结果排序,注意这里不能直接return
  171. }
  172. public AttributeCollection GetAttributes()
  173. {
  174. return TypeDescriptor.GetAttributes(this, true);
  175. }
  176. public string GetClassName()
  177. {
  178. return TypeDescriptor.GetClassName(this, true);
  179. }
  180. public string GetComponentName()
  181. {
  182. return TypeDescriptor.GetClassName(this, true);
  183. }
  184. public TypeConverter GetConverter()
  185. {
  186. return TypeDescriptor.GetConverter(this, true);
  187. }
  188. public EventDescriptor GetDefaultEvent()
  189. {
  190. return TypeDescriptor.GetDefaultEvent(this, true);
  191. }
  192. public PropertyDescriptor GetDefaultProperty()
  193. {
  194. return TypeDescriptor.GetDefaultProperty(this, true);
  195. }
  196. public object GetEditor(Type editorBaseType)
  197. {
  198. return TypeDescriptor.GetEditor(this, editorBaseType, true);
  199. }
  200. public EventDescriptorCollection GetEvents(Attribute[] attributes)
  201. {
  202. return TypeDescriptor.GetEvents(this, attributes, true);
  203. }
  204. public EventDescriptorCollection GetEvents()
  205. {
  206. return TypeDescriptor.GetEvents(this, true);
  207. }
  208. public PropertyDescriptorCollection GetProperties()
  209. {
  210. return TypeDescriptor.GetProperties(this, true);
  211. }
  212. public object GetPropertyOwner(PropertyDescriptor pd)
  213. {
  214. return this;
  215. }
  216. }
  217. }