110,535
社区成员
发帖
与我相关
我的任务
分享
class AuthorAttribute: Attribute
{
public string Name { get; set; }
public DateTime CreatedTime{ get; set; }
}
2 它可以与程序集,类,方法,字段等进行关联,此处关联ModuleA类
[Author(Name="隔壁老王")]
class ModuleA
{
}
3 通过反射进行调用,并做你需要的处理
class Program
{
static void Main(string[] args)
{
ModuleA mA = new ModuleA();
AuthorAttribute rA = (AuthorAttribute)Attribute.GetCustomAttribute(mA.GetType(), typeof(AuthorAttribute));
Console.WriteLine(rA.Name);//隔壁老王
Console.ReadKey();
}
}