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