Attribute Quick Guide

Source: Internet
Author: User

Copy From msdn, but make it much easier to find what you are looking.

The attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, Delegate, Enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.

Information provided by an attribute is also known as metadata. metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. for example,. net Framework predefines and uses attribute types to control run-time behavior, and some programming versions use Attribute types to represent language features not directly supported by. net Framework Common Type System.

Using system;
Using system. reflection;

Namespace customattrcs {
// An enumeration of animals. Start at 1 (0 = uninitialized ).
Public Enum animal {
// Pets.
Dog = 1,
Cat,
Bird,
}

// A custom attribute to allow a target to have a pet.
Public class animaltypeattribute: attribute {
// The constructor is called when the attribute is set.
Public animaltypeattribute (animal PET ){
Thepet = PET;
}

// Keep a variable internally...
Protected animal thepet;

//... And show a copy to the outside world.
Public animal pet {
Get {return thepet ;}
Set {thepet = PET ;}
}
}

// A test class where each method has its own pet.
Class animaltypetestclass {
[Animaltype (animal. Dog)]
Public void dogmethod (){}

[Animaltype (animal. Cat)]
Public void catmethod (){}

[Animaltype (animal. Bird)]
Public void birdmethod (){}
}

class democlass {
static void main (string [] ARGs) {
animaltypetestclass testclass = new animaltypetestclass ();
type = testclass. getType ();
// iterate through all the methods of the class.
foreach (methodinfo minfo in type. getmethods () {
// iterate through all the attributes for each method.
foreach (attribute ATTR in
attribute. getcustomattributes (minfo) {
// check for the animaltype attribute.
If (ATTR. getType () = typeof (animaltypeattribute)
console. writeline (
"method {0} has a pet {1} attribute. ",
minfo. name, (animaltypeattribute) ATTR ). pet);
}

}
}
}
}

/*
* Output:
* Method dogmethod has a pet dog attribute.
* Method catmethod has a pet cat attribute.
* Method birdmethod has a pet bird attribute.
*/

 

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.