一个特性的例子
代码
[global::System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class MyControlAttribute : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
readonly string positionalString;
// This is a positional argument
public MyControlAttribute(string positionalString)
{
this.positionalString = positionalString;
// TODO: Implement code here
throw new NotImplementedException();
}
public string PositionalString
{
get { return positionalString; }
}
// This is a named argument
public int NamedInt { get; set; }
public ControlTypes ControlName
{ get; set; }
public MyControlAttribute()
{
}
}
public class MyClass
{
[MyControl(ControlName = ControlTypes.TextBox)]
public string MyProperty
{ get; set; }
}
public enum ControlTypes
{
TextBox,
DropDownList,
Label,
}
sealed class MyControlAttribute : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
readonly string positionalString;
// This is a positional argument
public MyControlAttribute(string positionalString)
{
this.positionalString = positionalString;
// TODO: Implement code here
throw new NotImplementedException();
}
public string PositionalString
{
get { return positionalString; }
}
// This is a named argument
public int NamedInt { get; set; }
public ControlTypes ControlName
{ get; set; }
public MyControlAttribute()
{
}
}
public class MyClass
{
[MyControl(ControlName = ControlTypes.TextBox)]
public string MyProperty
{ get; set; }
}
public enum ControlTypes
{
TextBox,
DropDownList,
Label,
}