Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 LINQ ADO.NET JavaScript DataGridView GridView AjaxPro DevExpress 面向对象 Extjs XML HTML教程 Oracle jQuery WPF MVC 分页 Office2010 GDI+ Visual C++2010 Dojo MySQL VB.NET WCF4.0 ASPxGridView WinForm textbox Sql2005 cookie Discuz!NT checkbox WCF SQL经典语句 T-SQL F# asp.net VS2008 SQL Ajax DropDownList VS2008新特性 TreeView Access Flex 页面执行时间 字符串 DataSet VB2005 回调 动画 C#时间 游戏 ASP.NET MVC
当前位置: 主页 > vs2010教程 >

Visual Studio2010讲解C#4.0语法(3)--C#4.0中特性(Attributes)的使用方法(2)

时间:2010-08-06 18:35来源:未知 作者:admin 点击:

下面我给出在VS2010中操作Attributes的综合性实例:


首先打开Visual Studio2010创建一个基于C#的ConsoleApplication工程Attributes:


在Program.cs文件里写入如下代码:

using System;  
using System.Reflection;  
using System.Collections;  
 
//该IsTestedAttribute类是一个用户定义的自定义属性类。  
//它可应用于任何包括报关  
//- 类型(结构,类,枚举,委托)  
//- 成员(方法,字段,事件,属性,索引器)  
public class IsTestedAttribute : Attribute  
{  
    public override string ToString()  
    {  
        return "测试";  
    }  
}  
 
//该AuthorAttribute类是一个用户定义的属性类。  
//它可应用于类声明只和结构。  
//它需要一位不愿透露姓名的字符串参数(作者姓名)。  
//它有一个可选的命名的参数的版本,它是int类型。  
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]  
public class AuthorAttribute : Attribute  
{  
    // 此构造函数指定命名参数的属性类。  
    public AuthorAttribute(string name)  
    {  
        this.name = name;  
        this.version = 0;  
    }  
 
    //此属性是只读的(它没有固定的访问)  
    //所以它不能作为一个参数来命名这个attribute.class使用。  
    public string Name   
    {  
        get   
        {  
            return name;  
        }  
    }  
 
    //此属性是读写(它有一个set访问)  
    //所以它可以作为一个命名参数时,使用这种使用  
    //类作为属性类..  
    public int Version  
    {  
        get   
        {  
            return version;  
        }  
        set   
        {  
            version = value;  
        }  
    }  
 
    public override string ToString()  
    {  
        string value = "作者 : " + Name;  
        if (version != 0)  
        {  
            value += " 版本 : " + Version.ToString();  
        }  
        return value;  
    }  
 
    private string name;  
    private int version;  
}  
 
//这里你附加AuthorAttribute用户定义的属性  
// Account类。这位未透露姓名的字符串参数传递给  
// AuthorAttribute类的构造函数创建时的属性。  
[Author("Joe Programmer")]  
class Account  
{  
    //此方法是附加IsTestedAttribute自定义属性。  
    [IsTested]  
    public void AddOrder(Order orderToAdd)  
    {  
        orders.Add(orderToAdd);  
    }  
 
    private ArrayList orders = new ArrayList();  
}  
 
//附加AuthorAttribute和IsTestedAttribute自定义属性这个类。  
//注意使用Version命名的参数的AuthorAttribute。  
[Author("Jane Programmer", Version = 2), IsTested()]  
class Order  
{  
    //在这里添加内容 ...  
}  
 
class MainClass  
{  
   private static bool IsMemberTested(MemberInfo member)  
   {  
        foreach (object attribute in member.GetCustomAttributes(true))  
        {  
            if (attribute is IsTestedAttribute)  
            {  
               return true;  
            }  
        }  
      return false;  
   }  
 
    private static void DumpAttributes(MemberInfo member)  
    {  
        Console.WriteLine("属性 : " + member.Name);  
        foreach (object attribute in member.GetCustomAttributes(true))  
        {  
            Console.WriteLine(attribute);  
        }  
    }  
 
    public static void Main()  
    {  
        // 为Account类显示属性  
        DumpAttributes(typeof(Account));  
 
        // 显示测试成员名单  
        foreach (MethodInfo method in (typeof(Account)).GetMethods())  
        {  
            if (IsMemberTested(method))  
            {  
               Console.WriteLine("会员{0}已经测试!", method.Name);  
            }  
            else 
            {  
               Console.WriteLine("会员{0}还没测试!", method.Name);  
            }  
        }  
        Console.WriteLine();  
 
        // 显示Order类的属性  
        DumpAttributes(typeof(Order));  
 
        // 关于该命令类的方法显示属性  
        foreach (MethodInfo method in (typeof(Order)).GetMethods())  
        {  
           if (IsMemberTested(method))  
           {  
               Console.WriteLine("会员{0}已经测试!", method.Name);  
           }  
           else 
           {  
               Console.WriteLine("会员{0}还没测试!", method.Name);  
           }  
        }  
        Console.WriteLine();  
        Console.Read();  
    }  

按下F5开始调试,运行界面如下:


 

AnyClass.cs: Attribute 'Help' is not valid on this declaration type.
It is valid on 'class' declarations only.

Tags:vs2010 C#4.0
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价