The support for Attribute in. NET Framework is a brand new feature, which supports the attribute class from it. In yourProgramThe appropriate use of this class, or the flexible and clever use of this class, will give your program some ability that is difficult to achieve in the past programming. Let's look at an example: the popularity of Ajax has led to the release of various Ajax frameworks, and there are also a lot of Ajax frameworks provided by third parties. Here we will introduce an Ajax. dll.
I believe that my friends who have used Ajax. dll are amazed at its "magic" function. Ajax means that JS is used to call the server-side functions from the client and return the server-side processing results. before Ajax occurs, each step is submitted to the server and then returned. You need to pay attention to this point, that is, whether you are using Ajax. Or the common method. The business logic of our server-side method remains unchanged. For the convenience of Ajax. dll, we can develop the server in the same conventional way.Code, Method. If you find a method, such as user registration, you want it to provide the Ajax function called on the client. In this case, developers do not want to re-write a page and repeat this code in its pageload. For the client to call. This is too complicated.
We hope to implement it in a simple way. Ajax. dll provides us with such convenience. We only need to add a feature modifier before the function that provides Ajax functions to solve the problem! Very simple and convenient!
Here I am not promoting and introducing the use of Ajax. dll. It is used as an example to introduce a practical application of features. The most important role of feature attribute is to "Enhance" the function of the code without changing the business logic of our code.
The above is an example. Let's write a simple program to simulate this process!
First, write a custom attribute class to describe the features that require Ajax methods.
// This feature is only applicable to methods.
[Attributeusage (attributetargets. Method)]
Public Class Ajaxattribute: attribute
{
// Simulate two parameters for example only
Private Bool Asy; // Asynchronous or not
Private Bool Debugmode; // Debug mode or not
Public Ajaxattribute ( Bool Asy)
{
Asy = Asy;
}
Public Bool Asy
{
Get { Return Asy ;}
Set {Asy = Value ;}
}
Public BoolDebugmode
{
Get{ReturnDebugmode ;}
Set{Debugmode=Value ;}
}
}
The application code is as follows:
Class Class1
{
Static Void Main ( String [] ARGs)
{
Type T = Typeof (Class1 );
Foreach (Memberinfo m In T. getmethods ())
{
Foreach (Attribute In M. getcustomattributes ( True ))
{
If ( Is Ajaxattribute)
{
// When the program is running, it finds a method that requires Ajax functionality and then processes it accordingly! Process omitted. The following is a simulation
Console. writeline ( " The {1} method in the {0} class provides the Ajax function with the asynchronous attribute {2 }. " , T. Name, M. Name, (ajaxattribute) a). asy );
}
}
}
Console. Readline ();
}
// Functions that require Ajax functionality. Add the following features to inform the compiler.
[Ajax ( True , Debugmode = False )]
Public String Registeruser ()
{
Return " New User " ;
}
}
The running result is as follows:
The preceding example demonstrates a custom feature and how to read and retrieve a class that applies this feature!
In this way, without changing the existing functions of a class, we can know which methods in the class are the functions that the user wants to "provide Ajax call.
It can be understood that attribute is a role that adds additional information to an element (such as a class, method, attribute, and parameter. These additional information is sometimes very important to us!