Define and use your own features in C #. net

Source: Internet
Author: User

Basic concepts of Attribute
People around the world often ask, what is Attribute? What is its use? It seems that this program can run without it. In fact, in. Net, Attribute is a very important component. This article provides relevant materials for your reference.
 
First of all, we are sure that Attribute is a class. The following is the description of this class in the msdn document: You are allowed to add a description declaration similar to a keyword during the common language runtime, which is called attributes, it marks the elements in the program, such as the type, field, method, and attribute.
 
The metadata of the Attributes and Microsoft. NET Framework files is stored together and can be used to describe your code to the runtime, or affect the behavior of the application when the program is running.
 
In. NET, Attribute is used to deal with a variety of problems, such as serialization, program security features, prevent instant compilers from optimizing program code so that code can be easily debugged.
 
Complex and component-oriented business development. We hope that modern software development engineers will have more elastic design, rather than the previous design methods. Microsoft's. NET Framework attaches additional functions to a wide range of features through well-known Declarative Programming. In a software system, features can be used to enhance the system's elasticity. This is because features enhance the low coupling of functions. Therefore, you can customize your own feature classes and use these low-coupling features properly based on your own intentions.
 
Using the. NET Framework to write Windows programs has become very simple in many aspects. In many cases, the. NET Framework uses the. NET compiler to bind the metadata to the Assembly during compilation, making flexible Program Design easier. In fact, for. NET, it is possible to use embedded metadata to free us from the DLL hell.
 
Fortunately, the designers of the. NET Framework did not choose to elegantly hide the metadata. The designers gave us the reflection API. through reflection, A. NET program can view this metadata through programming. A program can reflect anything contained in a specific assembly, or all types and members contained in it. (The reflection content is to be continued. Please note)
 
Binding metadata to an executable Assembly provides many advantages. This allows the. NET assembly to be completely self-described. It also allows developers to share components across languages, removing the need for header files. (These header files will expire due to the relevant implementation code .)
 
It seems hard to believe that all the good. NET metadata is useless, just a lie. However, it does exist. In. NET, you can create the metadata of your specific program and apply the metadata to what you can imagine.
 
By using custom features, developers can define the metadata of their own specific programs. Because the value of these features will become another part of the metadata, bound to a set of programs. Therefore, the values of these custom features can be checked and used by the reflection API.
 
Problem: We often mention the attributes of a class. The values of these attributes can be used as features. So what is the real difference between attributes and custom features?
 
Through this article, you will learn how to customize features, how to apply features to your source code classes and methods, and how to use the reflection API to obtain and use the values of these features.
 
How do I use features during Common Language Runtime?

Before you begin to consider how to use your own defined feature classes, let's look at some standard features that are already used when running in public languages.
 
The [WebMethod] feature provides a simple example. It can convert any public method in the subclass derived from WebService into a part of the Web Service exposure method, you can simply add [WebMethod] to the method definition.
 
Public class SomeWebService: System. Web. Services. WebService
{
[WebMethod]
Public DataSet GetDailySales ()
{
// Request processing code
}
}
You just need to add the [WebMethod] feature to a method,. NET will handle all the other things for you in the background.
 
When the [Conditional] attribute is used for a given method, whether the method is callable depends on whether the specified preprocessing identifier is defined. For example, see the following code:
Public class SomeClass
{
[Conditional ("DEBUG")]
Public void UnitTest ()
{
// Unit test code
}
}

This code shows whether the UnitTest () method of this class is valid depends on whether the pre-processing identifier "DEBUG" is defined ). We may be more interested in what actually happens after using [Conditional. When the condition is invalid, the compiler will stop all calls to this method, compared to preprocessing commands with the same function # if... # endif, this method is more concise, and we don't need to do anything to use this function.
 
The positioning parameters and naming parameters are used for the feature. In the example using the [Conditional] feature, the specific symbol is the positioning parameter. Positioning parameters are mandatory and must be provided.
 
Let's go back to the example using the [WebMethod] feature. Let's take a look at the named parameters. This feature has a name parameter for Description, which can be used as follows:
[WebMethod (Description = "Sales volume")]
 
The name parameter is optional, and the value of the parameter must be followed by the parameter name. If a location parameter exists, you need to write the location parameter first, and then write the name parameter after the location parameter.
 
I will talk about it later.

Related Article

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.