using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
namespace Test
{
public class Myclass
{
public static void Main ()
{
HelpAttribute HelpAttr;
string assemblyName;
Process p = Process.GetCurrentProcess();
assemblyName = p.ProcessName + ".exe";
Assembly a = Assembly.LoadFrom(assemblyName);
Type b = a.GetType("Test.Class1");
foreach (Attribute attr in b.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
Type c = a.GetType("Test.Class2");
foreach (Attribute attr in c.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
Type d = a.GetType("Test.Class3");
foreach (Attribute attr in d.GetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
Console.WriteLine("Description of {0}:/n{1},Version={2}",
assemblyName,HelpAttr.Description,HelpAttr.Version);
}
}
}
}
[Help("This is Class1")]
public class Class1
{
}
[Help("This is Class2", Version = "1.0")]
public class Class2
{
}
[Help("This is Class3", Version = "2.0",
Description = "This is do-nothing class")]
public class Class3
{
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true,
Inherited = true)]
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
this.version = "No Version is defined for this class";
}
protected string description;
public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}
protected string version;
public string Version
{
get
{
return this.version;
}
set
{
this.version = value;
}
}
}
}
结果为:
Description of ConsoleApplication7.exe:
This is Class1,Version=No Version is defined for this class
Description of ConsoleApplication7.exe:
This is Class2,Version=1.0
Description of ConsoleApplication7.exe:
This is do-nothing class,Version=2.0