您的位置:首页 > 编程语言 > C#

(转载)C# Attribute 用法备忘

2011-11-28 14:26 369 查看
View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyFirstAttribute
{
class Program
{
static void Main(string[] args)
{
System.Reflection.MemberInfo info=typeof(TestClass);
MyAttribute myAttribute = Attribute.GetCustomAttribute(info, typeof(MyAttribute)) as MyAttribute;
Console.WriteLine(myAttribute.Author);
Console.WriteLine(myAttribute.Time);
Console.ReadLine();
}
}

#region MyAttribute
[AttributeUsage(AttributeTargets.Class)]
public class MyAttribute : Attribute
{
private string _author;
private string _time;

public MyAttribute(string author, string time)
{
_author = author;
_time = time;
}

public string Author
{
get { return _author; }
}
public string Time
{
get { return _time; }
}
}
#endregion

[My("zzy", "2009-3-3")]
class TestClass
{

}
}


参考:http://www.cnblogs.com/zzy0471/archive/2009/03/04/1403390.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: