using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Caching;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
///DeveloperAttribute 的摘要说明
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class DeveloperAttribute : Attribute
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//Private fields.
private string name;
private string level;
private bool reviewed;
private int nnn;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//This constructor defines two required parameters: name and level.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DeveloperAttribute(string name, string level)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name = name;
this.level = level;
this.reviewed = false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Define Name property.
//This is a read-only attribute.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public virtual string Name
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return name; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Define Level property.
//This is a read-only attribute.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public virtual string Level
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return level; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Define Reviewed property.
//This is a read/write attribute.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public virtual bool Reviewed
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return reviewed; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ reviewed = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Nnn
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return nnn; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ nnn = value; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
检索应用到不同范围的属性的多个实例
GetCustomAttributes 方法和 GetCustomAttribute 方法不搜索整个类和返回该类中某个属性的所有实例。相反,它们一次只搜索一个指定方法或成员。如果将具有同一属性的某个类应用到每个成员,并要检索应用到这些成员的所有属性值,则必须向 GetCustomAttributes 和 GetCustomAttribute 分别提供每个方法或成员。
下面的代码示例将类用作参数,并在类级别上以及该类的每个方法上搜索 DeveloperAttribute(先前已定义)。
using System;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[Developer("Joan Smith", "42", Reviewed = true)]
class MainApp
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public static void Main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Call function to get and display the attribute.
GetAttribute(typeof(MainApp));
}
public static void GetAttribute(Type t)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Get instance of the attribute.
DeveloperAttribute MyAttribute = (DeveloperAttribute) Attribute.GetCustomAttribute(t, typeof (DeveloperAttribute));
if(null == MyAttribute)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("The attribute was not found.");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Get the Name value.
Console.WriteLine("The Name Attribute is: {0}." , MyAttribute.Name);
//Get the Level value.
Console.WriteLine("The Level Attribute is: {0}." , MyAttribute.Level);
//Get the Reviewed value.
Console.WriteLine("The Reviewed Attribute is: {0}." , MyAttribute.Reviewed);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void GetAttribute(Type t)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
DeveloperAttribute att;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Get the class-level attributes.
//Put the instance of the attribute on the class level in the att object.
att = (DeveloperAttribute) Attribute.GetCustomAttribute (t, typeof (DeveloperAttribute));
if(null == att)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("No attribute in class {0}.\n", t.ToString());
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("The Name Attribute on the class level is: {0}.", att.Name);
Console.WriteLine("The Level Attribute on the class level is: {0}.", att.Level);
Console.WriteLine("The Reviewed Attribute on the class level is: {0}.\n", att.Reviewed);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Get the method-level attributes.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Get all methods in this class, and put them
//in an array of System.Reflection.MemberInfo objects.
MemberInfo[] MyMemberInfo = t.GetMethods();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Loop through all methods in this class that are in the
//MyMemberInfo array.
for(int i = 0; i < MyMemberInfo.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
att = (DeveloperAttribute) Attribute.GetCustomAttribute(MyMemberInfo[i], typeof (DeveloperAttribute));
if(null == att)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("No attribute in member function {0}.\n" , MyMemberInfo[i].ToString());
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("The Name Attribute for the {0} member is: {1}.", MyMemberInfo[i].ToString(), att.Name);
Console.WriteLine("The Level Attribute for the {0} member is: {1}.", MyMemberInfo[i].ToString(), att.Level);
Console.WriteLine("The Reviewed Attribute for the {0} member is: {1}.\n", MyMemberInfo[i].ToString(), att.Reviewed);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如果在方法级别或类级别上未找到 DeveloperAttribute 的任何实例,GetAttribute 方法将通知用户未找到任何属性,并显示不包含该属性的方法或类的名称。如果找到了属性,则控制台将显示 Name、Level 和 Reviewed 字段。
可使用 Type 类的成员获取已传递的类中的各个方法和成员。该示例首先查询 Type 对象以获取类级别的属性信息。然后使用 Type..::.GetMethods 将所有方法的实例放到 System.Reflection..::.MemberInfo 对象的一个数组中以检索方法级别的属性信息。您也可以使用 Type..::.GetProperties 方法检查属性 (Property) 级别上的属性 (Attribute),或使用 Type..::.GetConstructors 方法检查构造函数级别上的属性 (Attribute)。