自定义属性
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;
/// <summary>
///DeveloperAttribute 的摘要说明
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class DeveloperAttribute : Attribute
{
//Private fields.
private string name;
private string level;
private bool reviewed;
private int nnn;
//This constructor defines two required parameters: name and level.
public DeveloperAttribute(string name, string level)
{
this.name = name;
this.level = level;
this.reviewed = false;
}
//Define Name property.
//This is a read-only attribute.
public virtual string Name
{
get { return name; }
}
//Define Level property.
//This is a read-only attribute.
public virtual string Level
{
get { return level; }
}
//Define Reviewed property.
//This is a read/write attribute. 
public virtual bool Reviewed
{
get { return reviewed; }
set { reviewed = value; }
}
public int Nnn
{
get { return nnn; }
set { nnn = value; }
}
}

检索应用到不同范围的属性的多个实例
下面的代码示例将类用作参数,并在类级别上以及该类的每个方法上搜索 DeveloperAttribute(先前已定义)。
using System;
[Developer("Joan Smith", "42", Reviewed = true)]
class MainApp
{
public static void Main()
{
//Call function to get and display the attribute.
GetAttribute(typeof(MainApp));
}
public static void GetAttribute(Type t)
{
//Get instance of the attribute.
DeveloperAttribute MyAttribute = (DeveloperAttribute) Attribute.GetCustomAttribute(t, typeof (DeveloperAttribute));
if(null == MyAttribute)
{
Console.WriteLine("The attribute was not found.");
}
else
{
//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);
}
}
public static void GetAttribute(Type t)
{
DeveloperAttribute att;
//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)
{
Console.WriteLine("No attribute in class {0}.\n", t.ToString());
}
else
{
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);
} 
//Get the method-level attributes.
//Get all methods in this class, and put them
//in an array of System.Reflection.MemberInfo objects.
MemberInfo[] MyMemberInfo = t.GetMethods();
//Loop through all methods in this class that are in the
//MyMemberInfo array.
for(int i = 0; i < MyMemberInfo.Length; i++)
{
att = (DeveloperAttribute) Attribute.GetCustomAttribute(MyMemberInfo[i], typeof (DeveloperAttribute));
if(null == att)
{
Console.WriteLine("No attribute in member function {0}.\n" , MyMemberInfo[i].ToString());
}
else
{
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);
}
}
}

}

如果在方法级别或类级别上未找到 DeveloperAttribute 的任何实例,GetAttribute 方法将通知用户未找到任何属性,并显示不包含该属性的方法或类的名称。如果找到了属性,则控制台将显示 Name、Level 和 Reviewed 字段。
可使用


浙公网安备 33010602011771号