C # Attribute entry (1)

Source: Internet
Author: User
Introduction

Attributes is a brand new declarative information. We can not only define design-level information (such as help file and URL for documentation) through features, but also run-time information (such as associating XML with class ), we can also use features to build self-describing components. In this tutorial, we will see how to create and add features to various program entities and how to obtain feature information in the runtime environment.



Definition

As described in msdn -----

"A feature is an additional declarative information assigned to a declaration ."



Use pre-defined features

In C #, there is a small set of predefined features. Before learning how to create custom features (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 object is outdated and what entity should be used to replace it. Actually, you can write any text here. The second parameter tells the compiler to treat the obsolete program entity as an error. The default value is false, which means the compiler generates a warning.

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

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


Custom features (M attributes)

Now let's take a look at how to develop our own features.

First, we need to derive our own feature class from system. Attribute (a class inherited from the system. Attribute abstract class, whether directly or indirectly inherited, will become a feature class. The declaration of a feature class defines a new feature that can be placed on the Declaration ).

Using system;
Public class helpattribute: attribute
{
}
Whether you believe it or not, we have established a custom feature. Now we can use it to describe existing classes as if we used the obsolete attribute above.

[Help ()]
Public class anyclass
{
}
Note: using the attribute suffix for a feature class name is a convention. However, when we add features to a program entity, whether or not to include attribute suffixes is our freedom. The compiler first looks for the added feature class in the derived class of system. attribute. If not, the compiler adds the attribute suffix to continue searching.



So far, this feature has not played any role. Next we will add something to 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 added an attribute to the helpattribute feature class and we will search for it in the runtime environment in the subsequent sections.
(To be continued)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.