C # Attribute programming (2)

Source: Internet
Author: User
Actually, if you write your own attributes, you can understand the nunit implementation mechanism. nunit obtains the Chinese method and attributes of the test class through reflection, and then processes them accordingly.
Suppose there is an object OBJ
Type type = obj. GetType () first obtain the type of the object
Testfixtureattribute [] attributearray = (testfixtureattribute []) type. getcustomattributes (typeof (testfixtureattribute), false );
Get the attribute definitions for this object through reflection, such as the [testfixture] attribute for the test class in nunit.
Methodinfo minfo = type. getmethod ("method name");
Obtain the method information of the specified Method Name of the object.
Testattribute [] attributearray = (testattribute []) minfo. getcustomattributes (typeof (testattrites), false );
Obtain the attribute definitions for the method, such as the [test] attribute for the method in the nunit test class.
Nunit is actually in operation, it must be through type. the getmethods () method obtains information about all methods, and analyzes the attributes of the method to determine which methods need to be tested. For example, when it encounters the ignore attribute, it skips the test.
It can be inferred that the ignore attribute class is basically defined as follows:
[Attributeusage (attributetargets. Method)]
Public class ignoreattribute: attribute
{
Private string describe; // description
Public ignoreattre.pdf (string info)
{Describe = Info ;}
Public String describe;
{Get {...}; set {..};}
}
Setting attributetargets as method indicates that this attribute can only be used by methods. Using a constructor, you must enter some description information when using the ignore attribute, for example, [ignore ("pass test")]

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.