Niwalker (original). Net Framework is a new feature of Attribute, which supports the Attribute class from it. Use this class properly in your program, or flexible
Use this class, which will make your program get a certain ability to do in previous programming. Let's take an example: If you are a member of a project development group, you want to track the information of the project code check, usually you can save the code's check information in the database to query; or put information
Write to the code's annotation, which can read the code for information inspected while reading the code. We know that .NET components are described, then they can let the code to describe it.
What about being checked? This allows us to save information and code together, and you can get information through the self-description of the code. The answer is to use Attribute. The following steps and code tells you how to do: First, we create a custom attribute, and set our Attribute on the elements of the Class to get a class code check information.
Using system; using system.reflection;
[AttributeUSAGE (ATTRIBUTETARGETS.CLASS)] / / Remember the content of the last section? Public class codeReviewAttribute: system.attribute // Defines a codeReView attribute {private; // code check person private string date; // Check date private string command; // check results information
// Parameter constructor public codeReviewAttribute (string reviewer, string date) {this.reviewer = revIewer; this.date = date;}
Public String REVIEWER {Get {Return REVIEWER;}}
Public string date {get {return date;}}
PUBLIC STRING Comment {get {recomment = value;}}}
Our custom codeReViewAttribute does not differ from the ordinary class, which is derived from Attribute, and said that our Attribute can only apply to classes through AttributeUsage.
Elements.
The second step is to use our codeReviewAttribute. If we have a Jack written by Myclass, check the people niwalker, July 9, 2003, so we applied
Attribute is as follows:
[CodeReView ("Niwalker", "2003-7-9", Comment = "Jack code")] member definition of public class myclass {// class
When this code is compiled, the compiler calls the constructor of the CodeReViewAttribute and puts "NiWalker" and "2003-7-9" as the parameters of the constructor. Notice the parameter table
There is also an assignment of a Comment property. This is the unique way of Attribute. Here you can set more attribute public properties (if any), you need to point out .NET
Framework1.0 allows for assigning values to the Private property, but do not allow this in .NET Framework1.1, this can only be assigned to the properties of public.
The third step is to take out the information we need. This is achieved by the reflection of .NET, the knowledge of reflection is limited to the space, I don't plan to explain here, maybe I will write another article in the future.
Class test {static void main (string [] args) {system.reflection.memberinfo info = typeof (myclass); // Get information on the Myclass class by reflection
// get customized Attribute CodeReviewAttribute att exerted on the class MyClass = (CodeReviewAttribute) Attribute.GetCustomAttribute (info, typeof (CodeReviewAttribute)); if (! Att = null) {Console.WriteLine ( "Code Checker: {0}" , att.reviewer; console.writeline ("Check Time: {0}", Att.date); Console.Writeline ("Note: {0}", att.comment);}}}}
In this example, Attribute plays a role that adds additional information to a class, which does not affect the behavior of MyClass classes. Through this example, we can roughly know how to write
A custom Attribute, and how to use it in the app. In the next section, I will show how to use Attribute to generate the code of ADO.NET's data access class.