Understand the application of. Net custom attributes from nunit

Source: Internet
Author: User

Abstract:

The Custom Attributes of. Net can realize the extension of Assembly metadata, and provide new ideas and solutions for the framework design and implementation. Nunit is a good example.

1. Attribute Overview

. Net metadata system is one of the highlights of. NET Framework. Metadata can be used to describe the characteristics of an assembly, module, type, method, and member. These descriptions will be compiled into the Assembly as metadata and passed.. Net runtime environment is used by the caller. This is also the Metadata Extension.

The definition and use of attributes is the main content of Metadata Extension. This includes the use of. Net standard attributes and custom attributes.

Note: "attribute" is the translation of the concept-attribute discussed in this article by Chinese msdn. I am also used to this. This seems to be easily confused with the "attribute" in OO. In some similar discussions, this is translated as "tag" and "tag". Note the difference.

Developers may still feel unfamiliar with attributes and have no perceptual knowledge about how they play a role. However, in daily development,. NET developers are already using a large number of standard. NET attributes. For example:

To reference a function in a traditional dynamic connection library, we often use the standard attribute dllimportattribute to comment out the prototype of a function:

[Dllimport ("user32.dll")]

Public static extern int messageboxa (int p, string M, string H, int t );

//...
 

For example, the webserviceattribute and webmethodattribute attributes are used when you compile a web service. [WebService] and [webmethod] will play a role in generating the WSDL, and generate the corresponding web service description for us. How familiar are the developers of the following code snippets:

[Webmethod]

Public String helloworld ()

//...
 

These attributes directly comment on the type or type members in the Assembly, and such annotations can affect the compilation, logic, and running of the corresponding assembly .. Net Framework has many standard attributes and has detailed documentation in msdn.

2. Custom Attributes

You can also customize attributes, that is, you can develop attributes, define attributes in a certain format, and use attributes to influence the Assembly developed by developers. This is also the most direct application of. Net Metadata Extension. Custom Attributes are compiled into the Assembly as metadata during compilation, and can be read using the reflection mechanism during runtime.

. NET attributes are defined in the form of a class. Almost all attributes are inherited from system. Attribute. developer-defined attributes must first be inherited from this abstract class.

Custom Attributes can be used to mark and describe elements in an assembly and compiled into a. Net assembly to become part of its metadata. Reading from attributes and attribute values is the reading of. Net Assembly metadata, which uses the reflection mechanism. There are many examples of writing custom attributes and reading attributes in msdn.

3. Application of attributes

The above features of attributes are often useful when designing some frameworks: Using Reflection mechanisms, metadata as attributes can in turn affect the running configuration items of the code at runtime, or Mark special operation methods with properties for special processing at runtime. Another tempting application of properties is the tool that can be used to build and manage project assembly: properties are represented by some annotation, And the annotation content can be read from the Assembly after compilation, this allows you to manage various types and methods in a set of programs by commenting and reading attribute content.

3.1 attribute application in nunit

Let's take a look at the application of attributes in framework design! The most typical example is nunit. The nunit framework gives full play to the features of custom attributes and the reflection mechanism of. net. Take a simplified test case (testcase) as an example: During the test, nunit needs to run three different functions in sequence as follows:

First, prepare the function in the environment before running the test. Then, it is 0 ~ N test functions. Finally, clean up the functions in the test environment. Developers familiar with nunit know that nunit uses the [setup], [test], and [teardown] attributes in the testcase of nunit for marking. For example:

// Code of an nunit Test Program

[Setup]

Public void Init ()

//...

[Teardown]

Public void destroy ()

//...

[Test]

Public void testxxx ()

//...
 

The nunit framework should read and retrieve the above functions from the program to be tested at runtime, and ensure that the above three functions are called in the correct order. Nunit is implemented as follows:

First, a set of attributes are developed to mark various functions in the test case (testcase), such as [setup], [test], and [teardown]. (Nunit attribute markup is not only used to mark functions in the Assembly, but is limited to space. It is discussed only in the simplified environment previously)

During runtime, nunit uses the reflection mechanism to run functions in a test case (testcase) that has been compiled into an assembly. The nunit framework has a series of functions to do this. These functions are only responsible for running the functions marked by specific properties in the test case set. For example, invokesetup () is responsible for running the function labeled with [setup]; invoketestcase () is responsible for running the function labeled with [test], that is, the test case; invoketeardown () run the function marked with [teardown. Then nunit uses the call sequence of these invokexxx () functions to ensure the order in which the three functions run.

// From templatetestcase in nunit. Core namespace

// Run function used for testing

Public override void run (testcaseresult testresult)

{

//...

Try {

//...

Invokesetup (); // first run the function marked with [setup]

//...

Invoketestcase (); // then [test]

//...

}

Catch (...)

//...

Finally {

//...

Invoketeardown (); // The final function marked by [teardown]

//...

}

//...

}
 

The invokexxx () function uses the reflection mechanism to run related functions. You can look at the following code segments:

// From templatetestcase in nunit. Core namespace

Private void invokesetup ()

{

Methodinfo method = findsetupmethod (fixture); // gets the function reflection instance marked by [setup]

If (method! = NULL)

{

Invokemethod (method, fixture); // run this function

}

}
 

Findsetupmethod (...) By calling a findmethodbyattribute (...) Using the reflection mechanism to obtain the methodinfo that can call this function, and finally using invokemethod (methodinfo ,...) .

// From test class in nunit. Core namespace

Protected void invokemethod (methodinfo method, object fixture)

{

If (method! = NULL)

{

Try

{

Method. Invoke (fixture, null); // call the method or constructor reflected by the Method Instance

}

Catch (...)

//...

}

}
 

Read the source code carefully and you can see that nunit uses the reflection mechanism to run the test cases in the test assembly, therefore, there are specific requirements for the return values and parameters of the [setup], [test], and [teardown] functions, forming a rule coupling. This is a necessary design to facilitate reflection implementation and simplify the framework.

From nunit, we can see that the custom attributes in the. NET Metadata Extension are applied in the framework design. I believe more framework-class projects will take advantage of the. NET custom attributes. The following describes the relationships between several classes in nunit (simplified ).

Figure 2: simplified class diagrams of several classes using attribute definition and reading in nunit

3.2 attribute other applications

The excellent architecture inspires our thinking that the application of. Net attribute Metadata Extension is far more than that. Using its features, you can also implement several applications and framework designs. Recently, I am implementing a project code management tool based on property Metadata Extension. I will share it with my friends later.

4. Summary

Using. Net custom attributes to implement Metadata Extension can add new ideas and solutions for our design and architecture. Learning excellent frameworks (such as nunit) is a good way. However, reading metadata will inevitably involve the use of reflection mechanisms, while reflection mechanisms have low performance, which must be carefully designed and structured.

-End-

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.