C # learn the advanced features of the. NET Framework-how to customize attribute classes in the. NET Framework? (3)

Source: Internet
Author: User

  Reading directory

I. Attributes

Ii. Custom Attributes BookAttribute instances

.The attribute is implemented by the attribute class. In the previous article "C # Learn from the newest and learn from others. NET Framework advanced features -. the properties provided by the NET Framework (II). We are talking about the same Obsolete. We can also customize the attribute class. The Attribute Class is composed of System. derived from Attribute

  .For convenience, You can omit the Attribute after the Attribute name when using a custom Attribute in C #. For example: [BookAttribute ("custom Attribute class")] can be rewritten to [Books ("custom attribute class")]

  .Features of custom attribute classes

1: The AttributeUsage attribute can be used to limit the usage of the attribute.

The AttributeUsage attribute contains an enumeration named AttributeTargets, which can be used to specify where to use custom attributes. For example, the specified attribute is only used for classes and structures.

2: You can use the AttributeUsage attribute to specify whether the attribute can be used multiple times.

You can specify the value of the AllowMultiple parameter in the AttributeUsage attribute to determine whether the attribute can be used multiple times. If the value of AllowMultiple is true, the attribute can be used multiple times. If the value of AllowMultiple is false, the attribute can be used only once, for example: [AttributeUsage (AllowMultiple = true)]

3: You can set attribute parameters.

The parameter list given in the property class constructor is the property parameter.

  Ii. Custom Attributes BookAttribute instances

 1: BookAttribute. cs

Namespace CustomAttributeClass
{
// Only applicable to classes and attributes can be used multiple times
[AttributeUsage (AttributeTargets. Class, AllowMultiple = true)]
// Inherit from Attribute Class
Class BookAttribute: Attribute
{
Private string _ bookname; // private Field

/// <Summary>
/// Book name attributes
/// </Summary>
Public string BookNmae
{
Get {return _ bookname ;}
Set {_ bookname = value ;}
}
// Constructor, set attribute Parameters
Public BookAttribute (string strBookName)
{
This. _ bookname = strBookName;
}
}
 }

2: BookInfo. cs

Namespace CustomAttributeClass
{

// Use Attribute classes for multiple times
[Book ("ASP. NET in-depth analysis series")]
[Book ("C # Full learning manual")]
Class BookInfo
{
Public string bookinfo = "Book information is as follows ";
// Constructor
Public BookInfo ()
{

}
}
}

3: Program. cs

Static void Main (string [] args)
{
BookInfo bookinfo = new BookInfo ();
Object [] bookarr; // object Array
// Obtain the object type. Use the typeof method to retrieve the current class type, that is, BookInfo.
MemberInfo typeinfo = typeof (BookInfo );
// Call the GetCustomAttributes method to obtain the attribute list related to the BookAttribute class. This method returns an array of objects.
Bookarr = typeinfo. GetCustomAttributes (typeof (BookAttribute), false );
If (bookarr. GetLength (0 )! = 0)
{
BookAttribute bookattribute1 = (BookAttribute) bookarr [0]; // gets the first member of an attribute class
BookAttribute bookattribute2 = (BookAttribute) bookarr [1]; // gets the first member of an attribute class.
Console. WriteLine (bookinfo. bookinfo );
Console. WriteLine ("book name:" + bookattribute1.BookNmae + "-" + bookattribute2.BookNmae );
Console. ReadKey ();
}
}

  

  

 

 

Related Article

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.