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;
}