Introduction to attributes and definition attributes

Source: Internet
Author: User

Description attributes
Attribute provides a common access method for accessing User-Defined annotation information. Commenting on information is random. In other words, this information is not inherent in the language, but any information you can imagine. You can use attributes to define design-time information (such as documents), runtime information (such as database Field names), and runtime behavior characteristics (such as assuming that the Members are transaction processing, or can participate in transaction processing ). In a sense, associated information follows the same principle as XML development. Because you can create an Attribute Based on any information you need, an existing standard mechanism applies to defining attributes themselves and additional attributes that are suitable for querying members or types at runtime. Using attributes, we can add additional information to class members, so we can have a completely self-declared component.
Simply specify attribute data in brackets ([]) before the target type or member, you can attach a defined attribute to the C # type or member.

Define attributes
Attribute is actually a class derived from the class system. attribute. Class System. attribute contains some useful methods to customize attributes for access and test.
You do not need to include the suffix attribute When attaching an attribute to a type or member. This is a shortcut that is inserted with the help of the C # Language designer. When the compiler finds that an attribute is attached to a type or member, it automatically retrieves the system. Attribute derived class with the specified attribute name. If the compiler cannot locate a class, the compiler appends the attribute to the specified attribute name. Therefore, the general convention for defining an attribute class is: when defining an attribute class, it is ended with an attribute and some names are omitted.
Example:

Using system;
Using system. reflection;

Public Enum reghives
{
Hkey_classes_root,
HKEY_CURRENT_USER,
HKEY_LOCAL_MACHINE,
HKEY_USERS,
Hkey_current_config
}

Public class regkeyattribute: attribute
{
Public regkeyattribute (reghives hive, string valuename)
{
This. Hive = hive;
This. valuename = valuename;
}

Protected reghives hive;
Public reghives hive
{
Get {return hive ;}
Set {hive = value ;}
}

Protected string valuename;
Public String valuename
{
Get {return valuename ;}
Set {valuename = value ;}
}
}


[Regkey (reghives. HKEY_CURRENT_USER, "someclass")]
Class someclass
{
Public int Foo;
Public int bar;
}

Public class testreskeyattribute
{
Public static void main ()
{

}
}

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.