拮取自(This article is modified from) https://secure.codeproject.com/KB/cs/attributes.aspx?display=Print
using System;
using System.Reflection;
using System.Diagnostics;
class A
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
//attaching Help attribute to entire assembly
[assembly: Help("This Assembly demonstrates custom attributes creation and their run-time query.")]
//our custom attribute class
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description;
public String Description
{
get
{
return this.description;
}
}
}
//attaching Help attribute to our AnyClass
[Help("This is a do-nothing Class.")]
[Help("This is a do-nothing Class2")]
public class AnyClass
{
//attaching Help attribute to our AnyMethod
[Help("This is a do-nothing Method.")]
public void AnyMethod() {}
//attaching Help attribute to our AnyInt Field
[Help("This is any Integer.")]
public int AnyInt;
}
class QueryApp
{
public static void Main()
{
Type type = typeof(AnyClass);
HelpAttribute HelpAttr;
//Querying Class Attributes
foreach (Attribute attr in type.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
Console.WriteLine("Description of AnyClass:\n{0}", HelpAttr.Description);
}
//Querying Class-Method Attributes
foreach (MethodInfo method in type.GetMethods())
{
foreach (Attribute attr in method.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
Console.WriteLine("Description of {0}:\n{1}",method.Name, HelpAttr.Description);
}
}
//Querying Class-Field (only public) Attributes
foreach (FieldInfo field in type.GetFields())
{
foreach (Attribute attr in field.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
Console.WriteLine("Description of {0}:\n{1}", field.Name, HelpAttr.Description);
}
}
}
}
}
using System.Reflection;
using System.Diagnostics;
class A
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
//attaching Help attribute to entire assembly
[assembly: Help("This Assembly demonstrates custom attributes creation and their run-time query.")]
//our custom attribute class
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description;
public String Description
{
get
{
return this.description;
}
}
}
//attaching Help attribute to our AnyClass
[Help("This is a do-nothing Class.")]
[Help("This is a do-nothing Class2")]
public class AnyClass
{
//attaching Help attribute to our AnyMethod
[Help("This is a do-nothing Method.")]
public void AnyMethod() {}
//attaching Help attribute to our AnyInt Field
[Help("This is any Integer.")]
public int AnyInt;
}
class QueryApp
{
public static void Main()
{
Type type = typeof(AnyClass);
HelpAttribute HelpAttr;
//Querying Class Attributes
foreach (Attribute attr in type.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
Console.WriteLine("Description of AnyClass:\n{0}", HelpAttr.Description);
}
//Querying Class-Method Attributes
foreach (MethodInfo method in type.GetMethods())
{
foreach (Attribute attr in method.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
Console.WriteLine("Description of {0}:\n{1}",method.Name, HelpAttr.Description);
}
}
//Querying Class-Field (only public) Attributes
foreach (FieldInfo field in type.GetFields())
{
foreach (Attribute attr in field.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
Console.WriteLine("Description of {0}:\n{1}", field.Name, HelpAttr.Description);
}
}
}
}
}
印出結果:
Description of AnyClass:
This is a do-nothing Class2
Description of AnyClass:
This is a do-nothing Class.
Description of AnyMethod:
This is a do-nothing Method.
Description of AnyInt:
This is any Integer.
浙公网安备 33010602011771号