Getting started with C # Features (1)

xiaoxiao2021-03-06  40

C # properties (Attribute) entry (a) Author: cppbug www.ASPCool.com Time: 2004-4-2 21:03:29

By Sadaf Alvi Translation: CPPBUG (ITWizard@163.com) 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. Defining as described in MSDN --- "Features is an additional declarative information specified to a statement." Use a pre-defined feature in C #, there is a small pre- Define the characteristic 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"] Static Void NEW () {} PUBLIC VOID MAIN () {} public static void main () {} Let's take a look at the above example first, 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 customization characteristics (Custom Attributes now let us come See 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 {} No matter if you believe, we have established a customization feature, now we can use it to decorate the existing classes as if we are using Obsolete Attribute. [Help ()] public class anyclass {} Note: Use the Attribute suffix for a feature class name to be a practice. 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.

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

New Post(0)