Reflecting of Attribute via IL in CLR探索
2007-10-15 16:26
281 查看
关于Attribute的详细知识,我就不多说了.大家可以参考dudu和anytao的一个系列里面的介绍,比较经典的探讨了Attribute的一些生成和运行机制.
在这里,我只想从几个侧面来探讨下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表示的自定义属性的数组.
在这里,我只想从几个侧面来探讨下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表示的自定义属性的数组.
相关文章推荐
- Reflecting of Attribute via IL in CLR探索
- C# via CLR之IL中间语言初步探索
- C# via CLR之IL中间语言初步探索
- Set Visible attribute in the node of HTML for ASPX
- 探索CLR原理系列(3):方法元数据和IL(适合老鸟,新人勿沉迷其中)
- 读书笔记—CLR via C#委托和attribute
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_IL和验证
- Primitive, Reference, and Value Types(Chapter 5 of CLR via C#)
- How to know whether the attribute belongs to one types of productin Magento?
- Part 68 - What is the use of NonAction attribute in mvc
- Values of attribute 'level' not equal in different manifest snippets-MFC链接问题
- 探索CLR原理系列(3):方法元数据和IL(适合老鸟,新人勿沉迷其中)
- 探索CLR原理系列(3):方法元数据和IL(适合老鸟,新人勿沉迷其中)
- Type Fundamentals(Chapter 4 of CLR via C#)
- How can we specify src attribute of img tag in CSS?
- [PHP] To display some attribute of the node in the XML document.
- The CLR’s Execution Model(Chapter 1 of CLR via C#)
- White space is required before the attribute type in the declaration of attribute "t" for element "b
- [CLR via C#]17#Delegates WORK-IN-PROGRESS
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.