Turn from:
http://blog.csdn.net/nndtdx/article/details/6905802
http://www.cnblogs.com/luckdv/articles/Atrribute.html#3321438
Http://www.cnblogs.com/kai364/p/4417436.html
Http://www.cnblogs.com/mahaisong/category/326195.html
Http://www.cnblogs.com/fenrir/archive/2008/09/08/1286388.html
http://blog.csdn.net/yuwuji/article/details/6958304
http://www.cnblogs.com/ghfsusan/archive/2009/07/15/1524192.html
Http://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html
http://blog.csdn.net/pan_junbiao/article/details/8461703
Http://www.runoob.com/csharp/csharp-attribute.html
Http://www.cnblogs.com/xugang/archive/2011/01/06/1927619.html
What is the use of C # AttributeUsage? First, let's take a look at what is the AttributeUsage class, which is another pre-defined attribute class, and the AttributeUsage class is designed to help us control the use of custom features. In fact, the AttributeUsage class is a description of a custom feature such as and is used.
C # AttributeUsage use to understand:
AttributeUsage has three properties that we can put in front of custom properties. The first property is:
Validon
With this attribute, we are able to define which program entity the custom attribute should be placed before. A property can be placed by all program entities listed in AttributeTargets Enumerator. We can combine a number of attributetargets values by the or operation.
AllowMultiple
This property marks whether our custom features can be repeatedly placed in front of the same program entity.
Inherited
We can use this property to control the inheritance rules for custom attributes. It marks whether our attributes can be inherited.
Examples of use of C # AttributeUsage:
Let's do some practical things. We will place the AttributeUsage feature in front of the help feature to look forward to using it to control the use of the helper feature.
Using System;
[AttributeUsage (AttributeTargets.Class), AllowMultiple =False
inherited =false ]
public class helpattribute : attribute
{
{
this.description = description_in;
}
protected string description;
public string Description
{
get
{
return this.description;
}
}
}
Let's take a look at AttributeTargets.Class first. It specifies that the Help feature can only be placed in front of class. This also means that the following code will produce an error:
[Help ("This is a do-nothing class")]
Public class Anyclass
{
[Help ("This is a do-nothing method")] //error
public void Anymethod ()
{
}
}
The compiler reported an error as follows:
AnyClass.cs:Attribute ' help ' isn't valid on this declaration type.
It is valid on ' class ' declarations only.
We can use AttributeTargets.All to allow the Help attribute to be placed before any program entities. The possible values are:
assembly,
module,
class,
struct,
enum,
constructor,
method,
property,
Field,
event,
interface,
parameter,
delegate,
all = assembly | module | class |
Struct | Enum | Constructor |
Method | property | field | event |
interface | parameter | delegate,
classmembers = class | struct | enum |
constructor | method | property | field |
event | delegate | interface )
Consider AllowMultiple = False below. It specifies that the attribute cannot be placed repeatedly.
[Help ("This is a do-nothing class")]
[Help ("It contains a do-nothing method")]
Public class Anyclass
{
[Help ("This is a do-nothing method")] //error
public void Anymethod ()
{
}
}
It produces a compile-time error.
AnyClass.cs:Duplicate ' help ' attribute
Ok, now let's discuss the final attribute. Inherited, which indicates whether it can be inherited by a derived class when the attribute is placed on a base class.
[Help ("BaseClass")]
Public class Base
{
}
Public class Derive:base
{
}
The use of C # AttributeUsage will have four possible combinations:
[AttributeUsage (AttributeTargets.Class,
AllowMultiple = false, inherited = false]
[AttributeUsage (AttributeTargets.Class,
AllowMultiple = true, inherited = false]
[AttributeUsage (AttributeTargets.Class,
AllowMultiple = false, inherited = true]
[AttributeUsage (AttributeTargets.Class,
AllowMultiple = true, inherited = true]
C # AttributeUsage uses the first case:
If we query (we will see later how to query the attributes of a class at run time) derive class, we will find that the Help attribute does not exist because the inherited property is set to False.
C # AttributeUsage uses the second case:
Same as in the first case, because inherited is also set to false.
C # AttributeUsage uses the third case:
To explain the third and fourth cases, let's first add the point code to the derived class:
[Help ("BaseClass")]
Public class Base
{
}
[Help ("Deriveclass")]
Public class Derive:base
{
}
Now let's look at the Help feature, we can only get the properties of the derived class, because inherited is set to true, but AllowMultiple is set to false. Therefore, the help attribute of the base class is overridden by the derived class Help feature.
C # AttributeUsage uses the fourth case:
Here, we will find that the derived class has both the help attribute of the base class and its own Help feature, because AllowMultiple is set to true.
C # AttributeUsage The relevant content to introduce you here, I hope you understand and grasp the use of C # AttributeUsage to help.
AttributeUsage
"AttributeUsage"
System.AttributeUsage declares the scope and use of a attribute.
The AllowMultiple and inherited parameters are optional, so this code has the same effect:
The value of Attributetarget can be referenced in 1. Some of the desirable values are as follows:
If the allowmultiple parameter is set to True, the return attribute can be applied more than once to a single entity.
If inherited is set to false, the attribute is not inherited by a class that derives from the attributed class.
Attribute.GetCustomAttribute can get the Attribute of a class.
[AttributeUsage (AttributeTargets.Class)] public class Versionattribute:attribute {public string Name {get; set;} public string Date {get; set;} public string Describtion {get; set;} } [Version (Name = "Hyddd", Date = "2009-07-20", describtion = "Hyddd ' s Class")] public class MyCode { //... } class program { static void Main (string[] args) { var info = typeof (MyCode); var ClassAttribute = (versionattribute) Attribute.GetCustomAttribute (info, typeof (Versionattribute)); Console.WriteLine (classattribute.name); Console.WriteLine (classattribute.date); Console.WriteLine (classattribute.describtion); } }
[AttributeUsage (AttributeTargets.Class)] public class Versionattribute:attribute {public string Name {get; set;} public string Date {get; set;} public string Describtion {get; set;} } [Version (Name = "Hyddd", Date = "2009-07-20", describtion = "Hyddd ' s Class")] public class MyCode { //... } class program { static void Main (string[] args) { var info = typeof (MyCode); var ClassAttribute = (versionattribute) Attribute.GetCustomAttribute (info, typeof (Versionattribute)); Console.WriteLine (classattribute.name); Console.WriteLine (classattribute.date); Console.WriteLine (classattribute.describtion); } }
C # Features