1. 创建一个System.Attribute的子类
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method, //指定可应用于那些类型,如.Class, .Method, All等
AllowMultipe = true, //允许应用多次
Inherited = false //不继承到派生类
)]
public class SampleAttribute : System.Attribute
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private string descr;
private string author;
private string title;
public string Descr
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return descr;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{descr = value;}
}
public string Author
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return author ;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{author = value ;}
}
public string Title
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return title ;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{title = value ;}
}
//构造方法
Public SampleAttribute (string description)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
descr = description;
}
}
![](/Images/OutliningIndicators/None.gif)
在创建子类时,可以为该类标注上AttributeUsage特性. 同时指定新定义的自定义特性可以应用于哪些类型. 例如, 如果像下面这样给一个属性(property)添加刚建立的自定义特性, 编译器就会抛出异常, 提示”SampleAttribute对本声明类型无效,只对声明类和方法有效”
[SampleAttribute ("description for sample attribute", Author = "Test property")]
public string SomeProerty
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return "ok"}
}
![](/Images/OutliningIndicators/None.gif)
2. 使用定义好的自定义特性
[Sample ("description for sample attribute", Author = "Tester")]
public class TestApp
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public void Test ()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
Console.WriteLine ("Author {0}, Descr {1}", author, descr);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Sample(…)是SampleAttribute(…)的省略写法. 该代码会命名编译器调用自定义特性对象的构造函数, 并传入字符串的值(“description for sample attribute”由构造函数初始化), 然后再将”Author”属性设置为”Tester”(Author = “Tester”是由属性赋值)来完成一个特性对象的创建. 当然还可以设置其他的属性的值, 例如:
[Sample (“description for sample attribute”, Author = “Tester”, Title = “Title for this”, Descr = “cover with old descr” )]
3. 利用反射检索特性信息
public static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
MemberInfo info = typeof(TestApp); //TestApp是Test()所在的类名
SampleAttribute sample = (SampleAttribute)Attribute.GetCustomAttribute(info, typeof(SampleAttribute));
if(sample != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
Console.WriteLine("Author: {0}, Descr: {1} ", sample.Author, sample.Descr);
Console.ReadLine();
}
}
![](/Images/OutliningIndicators/None.gif)
完整代码下载