110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Reflection;
using System.Diagnostics;
//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 helpAttribute;
//Querying Assembly Attributes
String assemblyName;
Process process = Process.GetCurrentProcess();
assemblyName = process.ProcessName + ".exe";
Assembly assembly = Assembly.LoadFrom(assemblyName);
foreach (Attribute attribute in assembly.GetCustomAttributes(true))
{
helpAttribute = attribute as HelpAttribute;
if (null != helpAttribute)
{
Console.WriteLine("Description of {0}:\n{1}",
assemblyName, helpAttribute.Description);
}
}
Console.WriteLine("按任意键结束...");
Console.ReadKey();
}
}