Never give up

C#中自定义的Attribute类

上一篇 / 下一篇  2016-12-05 11:38:42

Creating Custom Attributes

The .Net Framework allows creation of custom attributes that can be used to store declarative information and can be retrieved at run-time. This information can be related to any target element depending upon the design criteria and application need.

Creating and using custom attributes involve four steps:

  • Declaring a custom attribute
  • Constructing the custom attribute
  • Apply the custom attribute on a target program element
  • Accessing Attributes Through Reflection

The Last step involves writing a simple program to read through the metadata to find various notations. Metadata is data about data or information used for describing other data. This program should use reflections for accessing attributes at runtime. This we will discuss in the next chapter.

Declaring a Custom Attribute

A new custom attribute should is derived from the System.Attribute class. For example,

//a custom attribute BugFix to be assigned to a class and its members[AttributeUsage(AttributeTargets.Class|AttributeTargets.Constructor|AttributeTargets.Field|AttributeTargets.Method|AttributeTargets.Property,AllowMultiple=true)]publicclassDeBugInfo:System.Attribute

In the preceding code, we have declared a custom attribute named DeBugInfo.

Constructing the Custom Attribute

Let us construct a custom attribute named DeBugInfo, which stores the information obtained by debugging any program. Let it store the following information:

  • The code number for the bug
  • Name of the developer who identified the bug
  • Date of last review of the code
  • A string message for storing the developer's remarks

The DeBugInfo class has three private properties for storing the first three information and a public property for storing the message. Hence the bug number, developer's name, and date of review are the positional parameters of the DeBugInfo class and the message is an optional or named parameter.

Each attribute must have at least one constructor. The positional parameters should be passed through the constructor. The following code shows the DeBugInfo class:

//a custom attribute BugFix to be assigned to a class and its members[AttributeUsage(AttributeTargets.Class|AttributeTargets.Constructor|AttributeTargets.Field|AttributeTargets.Method|AttributeTargets.Property,AllowMultiple=true)]publicclassDeBugInfo:System.Attribute{privateintbugNo;privatestringdeveloper;privatestringlastReview;publicstringmessage;publicDeBugInfo(intbg,stringdev,stringd){this.bugNo=bg;this.developer=dev;this.lastReview=d;}publicintBugNo{get{returnbugNo;}}publicstringDeveloper{get{returndeveloper;}}publicstringLastReview{get{returnlastReview;}}publicstringMessage{get{returnmessage;}set{message=value;}}}

Applying the Custom Attribute

The attribute is applied by placing it immediately before its target:

[DeBugInfo(45,"Zara Ali","12/8/2012",Message="Return type mismatch")][DeBugInfo(49,"Nuha Ali","10/10/2012",Message="Unused variable")]classRectangle{//member variablesprotecteddoublelength;protecteddoublewidth;publicRectangle(doublel,doublew){length=l;width=w;}[DeBugInfo(55,"Zara Ali","19/10/2012",Message="Return type mismatch")]publicdoubleGetArea(){returnlength*width;}[DeBugInfo(56,"Zara Ali","19/10/2012")]publicvoidDisplay(){Console.WriteLine("Length: {0}",length);Console.WriteLine("Width: {0}",width);Console.WriteLine("Area: {0}",GetArea());}}

In the next chapter, we retrieve attribute information using a Reflection class object.


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-29  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 21541
  • 日志数: 14
  • 建立时间: 2016-10-17
  • 更新时间: 2017-06-28

RSS订阅

Open Toolbar