Class Design: 一些有用的Attribute

来源:互联网 发布:淘宝会员折扣哪里设置 编辑:程序博客网 时间:2024/06/02 02:34
[Description("Damping calculation method"), CategoryAttribute("General propeller data")][TypeConverter(typeof(OptionConverter))][Range(0.0, 1.0, ErrorMessage = "PitchRatio must be between 0 and 1")][ReadOnly(false)]
[Browsable(false)][EditorBrowsable(EditorBrowsableState.Never)]
public double PitchRatio{ getset; }
 
[Range(1, 5, ErrorMessage = "PitchRatio must be between 1 and 5")][ReadOnly(false)]
[Browsable(false)][EditorBrowsable(EditorBrowsableState.Never)]
public int PitchRatio{ getset; }
1)PropertyGrid DataBinding支持/Array显示为Combox
[TypeConverter(typeof(OptionConverter))]
public class OptionConverter : StringConverter{        public static readonly string[] DampingModel = { "Dynamic magnifier""Schwanecke""Archer""Frahm" };        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)        {            return new StandardValuesCollection(DampingModel);        }         public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)        {            return true//True makes the Combobox select only. False allows free text entry.        }         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)        {            return true// True tells the PropertyGrid to display a Combobox        }}
2)Range
对于double, 要用[Range(0.0, 1.0, ErrorMessage = "PitchRatio must be between 0.0 and 1.0")]
如果用[Range(0, 1, ErrorMessage = "PitchRatio must be between 0.0 and 1.0")],则失效。
类型的准确。
3)[Browsable(false)]指定一个属性或事件是否应显示在“属性”窗口中。
4)[EditorBrowsable(EditorBrowsableState.Never)]指定某个属性或方法在编辑器中可以查看.
例如,Visual Studio 中的 IntelliSense 引擎使用此特性来确定是否显示属性或方法。 
自己定义显示AutoCompletionList;
5)[ReadOnly(false)],Description("Damping calculation method"), CategoryAttribute("General propeller data")]