天天看點

.net 特性使用

using System;

using System.Reflection;

public class HelpAttribute : Attribute

{

string url;

string topic;

public HelpAttribute(string url)

{

this.url = url;

}

public string Url

{

get { return url; }

}

public string Topic

{

get { return topic; }

set { topic = value; }

}

public string test { get; }

}

[Help("http://msdn.microsoft.com/.../MyClass.htm")]

public class Widget

{

[Help("http://msdn.microsoft.com/.../MyClass.htm", Topic = "Display")]

public void Display(string text) { }

}

class Test

{

static void ShowHelp(MemberInfo member)

{

HelpAttribute a = Attribute.GetCustomAttribute(member,

typeof(HelpAttribute)) as HelpAttribute;

if (a == null)

{

Console.WriteLine("No help for {0}", member);

}

else

{

Console.WriteLine("Help for {0}:", member);

Console.WriteLine(" Url={0}, Topic={1}", a.Url, a.Topic);

Console.ReadKey();

}

}

static void Main()

{

ShowHelp(typeof(Widget));

ShowHelp(typeof(Widget).GetMethod("Display"));

}

}