Application of Attribute in .NET Programming (2)

zhaozj2021-02-11  189

Application of Attribute in .NET Programming (2)

The support of Attribute in .NET Framework is a new feature that supports Attribute classes from it. Use this class appropriately in your program, or flexiblely use this class, which will make your program get a difficult ability in past programming. Let's take an example: If you are a member of a project development team, you want to track the information check of the project code, usually you can save the code's check information in the database to query; or write the information to the code Inside, this can read the code while seeing the information checked. We know that the component of the .NET is self-described, can you let the code you describe the information it 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 // Define a CodeReview Attribute

{

PRIVATE STRING REVIEWER; // Code check

Private string date; // check the date

Private string comment; // Check results information

// Parameter constructor

Public CodeReviewAttribute (String Reviewer, String Date)

{

THIS.REVIEWER = REVIEWER;

THIS.DATE = DATE;

}

Public String REVIEWER PUBLIC STRING

{

get

{

Return REVIEWER;

}

}

Public String Date

{

get

{

Return Date;

}

}

Public String Comment

{

get

{

Return Comment;

}

set

{

Comment = Value;

}

}

}

Our custom CodeReViewAttribute does not differ from the ordinary class, which is derived from Attribute, while attributeusage indicates that our Attribute can only be applied to the class element.

The second step is to use our CodeReviewAttribute, if we have a Jack written type myclass, check people niwalker, July 9, 2003, so we applied Attribute as follows:

[CodeReView ("Niwalker", "2003-7-9", Comment = "Jack code")]

Public Class Myclass

{

// member definition

}

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. Note that there is also an assignment of a Comment property in the parameter table. 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 Allow PRIVATE The attribute assignment, but in .NET Framework1.1 is not allowed to do so, only the value of the PUBLIC is assigned. 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 applied to the Myclass class

CodeReViewAttribute Att =

Attribute.getCustomattribute (INFO, TYPEOF (CodeReViewAttribute);

IF (att! = null)

{

Console.writeline ("Code Check: {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. With 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.

(to be continued)

转载请注明原文地址:https://www.9cbs.com/read-4186.html

New Post(0)