你把这个运行一下看看,我试了一下,OK的!//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) { // // TODO: Add constructor logic here 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.")] 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); } } } } }
[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)
{
//
// TODO: Add constructor logic here
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.")]
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);
}
}
}
}
}
这篇C#自定义属性里面也提到在程序运行时获取属性信息。
而“启动调试”和“开始执行(不调试)”的区别在于后者是直接执行编译后的程序,前者是加载到调试器(Debugger)上执行。