Reflecting of Attribute via IL in CLR探索
在这里,我只想从几个侧面来探讨下CLR环境下的Attribute.
首先,我们来看一个Reflecting in Attribute的例子:
using System;
using System.Reflection;
public class AttributesReflectingonAttributes
{
public static void Main()
{
Type type = typeof(Complex);
foreach (CodeReviewAttribute att in
type.GetCustomAttributes(typeof(CodeReviewAttribute), false))
{
Console.WriteLine("Reviewer: {0}", att.Reviewer);
Console.WriteLine("Date: {0}", att.Date);
Console.WriteLine("Comment: {0}", att.Comment);
}
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple=true)]
public class CodeReviewAttribute: System.Attribute
{
public CodeReviewAttribute(string reviewer, string date)
{
this.reviewer = reviewer;
this.date = date;
}
public string Comment
{
get
{
return(comment);
}
set
{
comment = value;
}
}
public string Date
{
get
{
return(date);
}
}
public string Reviewer
{
get
{
return(reviewer);
}
}
string reviewer;
string date;
string comment;
}
[CodeReview("AA", "01-12-2000", Comment="Joe' Code")]
[CodeReview("BB", "01-01-2000", Comment="Revisit this section")]
class Complex
{
}
这样,我们在这个例子中得到的输出结果是把Complex类的CodeReview这个Attribute里面的参数给输出来了.
System的Type这个类中的GetCustomAttributes方法,在派生类中被重写的时候,返回由System.Type表示的自定义属性的数组.
posted on 2007-10-15 16:26 lbq1221119 阅读(466) 评论(0) 编辑 收藏 举报