自定义属性

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; }
    }

}



检索应用到不同范围的属性的多个实例

GetCustomAttributes 方法和 GetCustomAttribute 方法不搜索整个类和返回该类中某个属性的所有实例。相反,它们一次只搜索一个指定方法或成员。如果将具有同一属性的某个类应用到每个成员,并要检索应用到这些成员的所有属性值,则必须向 GetCustomAttributesGetCustomAttribute 分别提供每个方法或成员。

下面的代码示例将类用作参数,并在类级别上以及该类的每个方法上搜索 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 方法将通知用户未找到任何属性,并显示不包含该属性的方法或类的名称。如果找到了属性,则控制台将显示 NameLevelReviewed 字段。

可使用 Type 类的成员获取已传递的类中的各个方法和成员。该示例首先查询 Type 对象以获取类级别的属性信息。然后使用 Type..::.GetMethods 将所有方法的实例放到 System.Reflection..::.MemberInfo 对象的一个数组中以检索方法级别的属性信息。您也可以使用 Type..::.GetProperties 方法检查属性 (Property) 级别上的属性 (Attribute),或使用 Type..::.GetConstructors 方法检查构造函数级别上的属性 (Attribute)。


posted on 2008-07-17 16:01  执法长老  阅读(309)  评论(0编辑  收藏  举报

导航