Custom Attributes in C #

Source: Internet
Author: User
1. What are custom attributes?
At that time, custom attributes are additional descriptions of classes, fields, and so on.
For example, in nunit, for the method to be tested using nunit, we will add [setup] [test] and other attributes before this method.
In this way, when running nunint, nunit can pass the attributes to know which methods are used for testing ..

Ii. How to customize attributes

Class alias: system. Attribute
{
String [] names;

Public alias (string [] Names)
{
This. Names = names;
}

Public String [] Names
{
Get {return names ;}
Set {names = values ;}
}


}

3. How to retrieve custom attributes from a defined Attribute Class

private dictionary getaliaslisting (type destinationtype)
{< br> // get all the properties that are in the
// destination type.
propertyinfo [] destinationproperties = destinationtype. getproperties ();
dictionary aliases = newdictionary ();
for each (propertyinfo property in destinationproperties)
{< br> // get the alias attributes.
object [] aliasattributes =
property. getcustomattributes (typeof (alias), true);
// loop through the alias attributes and
// Add them to the dictionary.
foreach (Object attribute in aliasattributes)
foreach (string name in (alias) Attribute ). names)
aliases. add (name, property. name);
// we also need to add the property name
// as an alias.
aliases. add (property. name, property. name);
}< br> return aliases;
}

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.