.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"));
}
}

posted @ 2013-05-31 15:46  易独  阅读(153)  评论(0)    收藏  举报
乐巴儿 一个有声音的公众号
长按,识别二维码,加关注