using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OTSModelSharp { public enum enumType { BOOLVAL, DIGITALVAL, STRINGVAL, CHECKVAL, RATIOVAL, IPVAL, COMBOBOX, RESETBTN }; public class XProp { private string theId = ""; //属性Id,我的项目中需要,大家可以忽略 private string theCategory = ""; //属性所属类别 private string theName = ""; //属性名称 private bool theReadOnly = false; //属性的只读性,true为只读 private string theDescription = ""; //属性的描述内容 private object theValue = null; //值 private System.Type theType = null; //类型 private bool theBrowsable = true; //显示或隐藏,true为显示 TypeConverter theConverter = null; //类型转换 //Combobox private Color _color; private Font _font; // private string strdemo; public string Id { get { return theId; } set { theId = value; } } public string Category { get { return theCategory; } set { theCategory = value; } } public bool ReadOnly { get { return theReadOnly; } set { theReadOnly = value; } } public string Name { get { return this.theName; } set { this.theName = value; } } public object Value { get { return this.theValue; } set { this.theValue = value; } } public Color Color { get { return _color; } set { _color = value; } } public Font Font { get { return _font; } set { _font = value; } } public string Description { get { return theDescription; } set { theDescription = value; } } public System.Type ProType { get { return theType; } set { theType = value; } } public bool Browsable { get { return theBrowsable; } set { theBrowsable = value; } } public virtual TypeConverter Converter { get { return theConverter; } set { theConverter = value; } } } public class XProps : List, ICustomTypeDescriptor { #region ICustomTypeDescriptor 成员 public AttributeCollection GetAttributes() { return TypeDescriptor.GetAttributes(this, true); } public string GetClassName() { String strname = TypeDescriptor.GetClassName(this, true); return strname; } public string GetComponentName() { String CompnentName = TypeDescriptor.GetComponentName(this, true); return CompnentName; } public TypeConverter GetConverter() { return TypeDescriptor.GetConverter(this, true); } public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); } public PropertyDescriptor GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); } public object GetEditor(System.Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); } public EventDescriptorCollection GetEvents(System.Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); } public PropertyDescriptorCollection GetProperties(System.Attribute[] attributes) { ArrayList props = new ArrayList(); for (int i = 0; i < this.Count; i++) { //判断属性是否显示 if (this[i].Browsable == true) { XPropDescriptor psd = new XPropDescriptor(this[i], attributes); props.Add(psd); } } PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropertyDescriptor)); return new PropertyDescriptorCollection(propArray); } public PropertyDescriptorCollection GetProperties() { return TypeDescriptor.GetProperties(this, true); } public object GetPropertyOwner(PropertyDescriptor pd) { return this; } #endregion public override string ToString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < this.Count; i++) { sb.Append("[" + i + "] " + this[i].ToString() + System.Environment.NewLine); } return sb.ToString(); } } public class XPropDescriptor : PropertyDescriptor { XProp theProp; public XPropDescriptor(XProp prop, Attribute[] attrs) : base(prop.Name, attrs) { theProp = prop; } public override bool CanResetValue(object component) { return false; } public override string Category { get { return theProp.Category; } } public override string Description { get { return theProp.Description; } } public override TypeConverter Converter { get { return theProp.Converter; } } public override System.Type ComponentType { get { return this.GetType(); } } public override object GetValue(object component) { return theProp.Value; } public override bool IsReadOnly { get { return theProp.ReadOnly; } } public override System.Type PropertyType { get { return theProp.ProType; } } public override void ResetValue(object component) { } public override void SetValue(object component, object value) { theProp.Value = value; } public override bool ShouldSerializeValue(object component) { return false; } } }