XProp.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OTSModelSharp
  10. {
  11. public enum enumType
  12. {
  13. BOOLVAL,
  14. DIGITALVAL,
  15. STRINGVAL,
  16. CHECKVAL,
  17. RATIOVAL,
  18. IPVAL,
  19. COMBOBOX,
  20. RESETBTN
  21. };
  22. public class XProp
  23. {
  24. private string theId = ""; //属性Id,我的项目中需要,大家可以忽略
  25. private string theCategory = ""; //属性所属类别
  26. private string theName = ""; //属性名称
  27. private bool theReadOnly = false; //属性的只读性,true为只读
  28. private string theDescription = ""; //属性的描述内容
  29. private object theValue = null; //值
  30. private System.Type theType = null; //类型
  31. private bool theBrowsable = true; //显示或隐藏,true为显示
  32. TypeConverter theConverter = null; //类型转换 //Combobox
  33. private Color _color;
  34. private Font _font;
  35. // private string strdemo;
  36. public string Id
  37. {
  38. get { return theId; }
  39. set { theId = value; }
  40. }
  41. public string Category
  42. {
  43. get { return theCategory; }
  44. set { theCategory = value; }
  45. }
  46. public bool ReadOnly
  47. {
  48. get { return theReadOnly; }
  49. set { theReadOnly = value; }
  50. }
  51. public string Name
  52. {
  53. get { return this.theName; }
  54. set { this.theName = value; }
  55. }
  56. public object Value
  57. {
  58. get { return this.theValue; }
  59. set { this.theValue = value; }
  60. }
  61. public Color Color
  62. {
  63. get { return _color; }
  64. set { _color = value; }
  65. }
  66. public Font Font
  67. {
  68. get { return _font; }
  69. set { _font = value; }
  70. }
  71. public string Description
  72. {
  73. get { return theDescription; }
  74. set { theDescription = value; }
  75. }
  76. public System.Type ProType
  77. {
  78. get { return theType; }
  79. set { theType = value; }
  80. }
  81. public bool Browsable
  82. {
  83. get { return theBrowsable; }
  84. set { theBrowsable = value; }
  85. }
  86. public virtual TypeConverter Converter
  87. {
  88. get { return theConverter; }
  89. set { theConverter = value; }
  90. }
  91. }
  92. public class XProps : List<XProp>, ICustomTypeDescriptor
  93. {
  94. #region ICustomTypeDescriptor 成员
  95. public AttributeCollection GetAttributes()
  96. {
  97. return TypeDescriptor.GetAttributes(this, true);
  98. }
  99. public string GetClassName()
  100. {
  101. String strname = TypeDescriptor.GetClassName(this, true);
  102. return strname;
  103. }
  104. public string GetComponentName()
  105. {
  106. String CompnentName = TypeDescriptor.GetComponentName(this, true);
  107. return CompnentName;
  108. }
  109. public TypeConverter GetConverter()
  110. {
  111. return TypeDescriptor.GetConverter(this, true);
  112. }
  113. public EventDescriptor GetDefaultEvent()
  114. {
  115. return TypeDescriptor.GetDefaultEvent(this, true);
  116. }
  117. public PropertyDescriptor GetDefaultProperty()
  118. {
  119. return TypeDescriptor.GetDefaultProperty(this, true);
  120. }
  121. public object GetEditor(System.Type editorBaseType)
  122. {
  123. return TypeDescriptor.GetEditor(this, editorBaseType, true);
  124. }
  125. public EventDescriptorCollection GetEvents(System.Attribute[] attributes)
  126. {
  127. return TypeDescriptor.GetEvents(this, attributes, true);
  128. }
  129. public EventDescriptorCollection GetEvents()
  130. {
  131. return TypeDescriptor.GetEvents(this, true);
  132. }
  133. public PropertyDescriptorCollection GetProperties(System.Attribute[] attributes)
  134. {
  135. ArrayList props = new ArrayList();
  136. for (int i = 0; i < this.Count; i++)
  137. { //判断属性是否显示
  138. if (this[i].Browsable == true)
  139. {
  140. XPropDescriptor psd = new XPropDescriptor(this[i], attributes);
  141. props.Add(psd);
  142. }
  143. }
  144. PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor));
  145. return new PropertyDescriptorCollection(propArray);
  146. }
  147. public PropertyDescriptorCollection GetProperties()
  148. {
  149. return TypeDescriptor.GetProperties(this, true);
  150. }
  151. public object GetPropertyOwner(PropertyDescriptor pd)
  152. {
  153. return this;
  154. }
  155. #endregion
  156. public override string ToString()
  157. {
  158. StringBuilder sb = new StringBuilder();
  159. for (int i = 0; i < this.Count; i++)
  160. {
  161. sb.Append("[" + i + "] " + this[i].ToString() + System.Environment.NewLine);
  162. }
  163. return sb.ToString();
  164. }
  165. }
  166. public class XPropDescriptor : PropertyDescriptor
  167. {
  168. XProp theProp;
  169. public XPropDescriptor(XProp prop, Attribute[] attrs) : base(prop.Name, attrs)
  170. {
  171. theProp = prop;
  172. }
  173. public override bool CanResetValue(object component)
  174. {
  175. return false;
  176. }
  177. public override string Category
  178. {
  179. get { return theProp.Category; }
  180. }
  181. public override string Description
  182. {
  183. get { return theProp.Description; }
  184. }
  185. public override TypeConverter Converter
  186. {
  187. get { return theProp.Converter; }
  188. }
  189. public override System.Type ComponentType
  190. {
  191. get { return this.GetType(); }
  192. }
  193. public override object GetValue(object component)
  194. {
  195. return theProp.Value;
  196. }
  197. public override bool IsReadOnly
  198. {
  199. get { return theProp.ReadOnly; }
  200. }
  201. public override System.Type PropertyType
  202. {
  203. get { return theProp.ProType; }
  204. }
  205. public override void ResetValue(object component)
  206. {
  207. }
  208. public override void SetValue(object component, object value)
  209. {
  210. theProp.Value = value;
  211. }
  212. public override bool ShouldSerializeValue(object component)
  213. {
  214. return false;
  215. }
  216. }
  217. }