Getting started with C # Features (1)

xiaoxiao2021-03-06  40

By sadaf alvi translation: cppbug (itwizard@163.com)

introduction

Features (attributes) is a new declarative information. Not only can we define information-level information (such as HELP FILE, URL for Documentation), and run-time information (such as Run-Time), and we can also use features to establish a self-description (Self -describing component. In this tutorial, we will see how to build and add features to various program entities and how to get feature information in the runtime environment.

definition

As described in MSDN -----

"The feature is an additional declarative information assigned to a certain statement."

Use pre-defined feature

In C #, there is a small predefined feature collection. Before learning how to build our own customizers (Custom Attributes), let's take a look at how to use predefined features in our code.

Using system;

Public class anyclass

{

[Obsolete ("Don't Use Old Method, Use New Method", True]

Static void old () {}

Static void new () {}

Public static void main ()

{

OLD ();

}

}

Let's take a look at the above example, in this example we use the Obsolete feature, which marks a program entity that should not be used again. The first parameter is a string that explains why the entity is outdated and what entity should be used instead of it. In fact, you can write any text here. The second parameter tells the compiler that should use this outdated program entity as an error. Its default is False, that is, a compiler will generate a warning.

When we try to compile the above programs, we will get an error:

Anyclass.old () 'is obsolete:' don't use old method, use new method '

Development of customizers (Custom Attributes)

Let us now look at how to develop our own characteristics.

First we have to derive our own feature class from System.attribute (a class from the SYSTEM.ATRIBUTE abstraction class, whether it is direct or indirect inheritance, will become a characteristic class. The statement of the characteristic class defines a kind of Placed on a new feature on a statement).

Using system;

Public Class Helpattribute: Attribute

{

}

Regardless of whether you believe, we have established a customization feature, now we can use it to decorate the existing classes as if we use Obsolete Attribute.

[Help ()]

Public class anyclass

{

}

Note: Using the Attribute suffix for a feature class name is a convention. However, when we add characteristics to a program entity, we include an Attribute suffix is ​​our freedom. The compiler will first find the added feature class in the derived class of System.attribute. If not found, the compiler will add the Attribute suffix to continue lookup.

So far, this feature has not played. Let's add something to it make it more useful.

Using system;

Public Class Helpattribute: Attribute

{

Public Helpattribute (String Descrition_IN) {

THIS.DESCRIPTION = Description_IN;

}

Protected string description;

Public String Description

{

get

{

Return this.description;

}

}

}

[Help ("this is a do-nothing class"]

Public class anyclass

{

}

In the above example, we add an attribute to the Helpattribute feature class and we will look up in the runtime environment in the subsequent section.

(Endlessly)

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

New Post(0)