C#---->特性&反射

特性Attribute

  • 特性是用于运行时传递程序中的各种元素(比如类,方法,结构,枚举,组件等)的行为信息的声明性标签
  • 可以使用特性向程序添加声明性信息,用来修饰各种需要被修饰的目标。我们可以对类、以及C#程序集中的成员进行进一步的描述。
  • 特性用于添加元数据,如编译器指令,方法,类,注释等其他信息
  • .Net框架提供了2种类型的特性:预定义特性和自定义特性

规定特性

Attribute

[attribute(positional_parameters,name_parameter=value,...)]
element

特性Attribute的名称和值都在括号内进行规定,放置在它所要应用的元素之前
positional_parameters 规定必需的信息,name_parameter 规定可选的信息。

预定义特性

AttributeUsage描述了如何使用一个自定义特性类。
Conditional这个预定义特性标记了一个条件方法,其执行依赖于指定的预处理标识符。
Obsolete标记了不应被使用的程序实体

自定义特性

  • 创建自定义特性(Attribute)
    .Net 框架允许创建自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用程序需要,可与任何目标元素相关。

  • 创建并使用自定义特性包含四个步骤:

    1. 声明自定义特性
    2. 构建自定义特性
    3. 在目标程序元素上应用自定义特性
    4. 通过反射访问特性

    最后一个步骤包含编写一个简单的程序来读取元数据以便查找各种符号。元数据是用于描述其他数据的数据和信息。该程序应使用反射来在运行时访问特性。

  1. 声明自定义特性
    一个新的自定义特性应派生自 System.Attribute 类。例如:

    // 一个自定义特性 BugFix 被赋给类及其成员
    [AttributeUsage(AttributeTargets.Class |
    AttributeTargets.Constructor |
    AttributeTargets.Field |
    AttributeTargets.Method |
    AttributeTargets.Property,
    AllowMultiple = true)]
    
    public class DeBugInfo : System.Attribute
    

    如此便声明了一个名为 DeBugInfo 的自定义特性。

  2. 构建自定义特性
    构建 DeBugInfo 自定义特性,该特性将存储调试程序获得的信息。它存储下面的信息:

    bug 的代码编号
    辨认该 bug 的开发人员名字
    最后一次审查该代码的日期
    一个存储了开发人员标记的字符串消息

    我们的 DeBugInfo 类将带有三个用于存储前三个信息的私有属性(property)和一个用于存储消息的公有属性(property)。所以 bug 编号、开发人员名字和审查日期将是 DeBugInfo 类的必需的定位( positional)参数,消息将是一个可选的命名(named)参数。

    每个特性必须至少有一个构造函数。必需的定位( positional)参数应通过构造函数传递。下面的代码演示了 DeBugInfo 类:
    实例

    // 一个自定义特性 BugFix 被赋给类及其成员
    [AttributeUsage(AttributeTargets.Class |
    AttributeTargets.Constructor |
    AttributeTargets.Field |
    AttributeTargets.Method |
    AttributeTargets.Property,
    AllowMultiple = true)]
    
    public class DeBugInfo : System.Attribute
    {
      private int bugNo;
      private string developer;
      private string lastReview;
      public string message;
    
      public DeBugInfo(int bg, string dev, string d)
      {
          this.bugNo = bg;
          this.developer = dev;
          this.lastReview = d;
      }
    
      public int BugNo
      {
          get
          {
              return bugNo;
          }
      }
      public string Developer
      {
          get
          {
              return developer;
          }
      }
      public string LastReview
      {
          get
          {
              return lastReview;
          }
      }
      public string Message
      {
          get
          {
              return message;
          }
          set
          {
              message = value;
          }
      }
    }
    
  3. 应用自定义特性

    通过把特性放置在紧接着它的目标之前,来应用该特性:

    [DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
    [DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
    class Rectangle
    {
      // 成员变量
      protected double length;
      protected double width;
      public Rectangle(double l, double w)
      {
          length = l;
          width = w;
      }
      [DeBugInfo(55, "Zara Ali", "19/10/2012",
      Message = "Return type mismatch")]
      public double GetArea()
      {
          return length * width;
      }
      [DeBugInfo(56, "Zara Ali", "19/10/2012")]
      public void Display()
      {
          Console.WriteLine("Length: {0}", length);
          Console.WriteLine("Width: {0}", width);
          Console.WriteLine("Area: {0}", GetArea());
      }
    }
    

反射

反射指程序可以访问、检测和修改它本身状态或行为的一种能力。
程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。
您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。
反射(Reflection)的用途

反射(Reflection)用途

1.允许在运行时查看特性(attribute)信息。
2.允许审查集合中的各种类型,以及实例化这些类型。
3.允许延迟绑定的方法和属性(property)。
4.允许在运行时创建新类型,然后使用这些类型执行一些任务。
using System;
using System.Collections;
using System.Reflection;
namespace BugFixApplication
{
    // 一个自定义特性 BugFix 被赋给类及其成员
    [AttributeUsage(AttributeTargets.Class |
    AttributeTargets.Constructor |
    AttributeTargets.Field |
    AttributeTargets.Method |
    AttributeTargets.Property,
    AllowMultiple = true)]

    public class DeBugInfo : System.Attribute
    {
        private int bugNo;
        private string developer;
        private string lastReview;
        public string message;

        public DeBugInfo(int bg, string dev, string d)
        {
            this.bugNo = bg;
            this.developer = dev;
            this.lastReview = d;
        }

        public int BugNo
        {
            get
            {
                return bugNo;
            }
        }
        public string Developer
        {
            get
            {
                return developer;
            }
        }
        public string LastReview
        {
            get
            {
                return lastReview;
            }
        }
        public string Message
        {
            get
            {
                return message;
            }
            set
            {
                message = value;
            }
        }
    }
    [DeBugInfo(45, "Zara Ali", "12/8/2012",
         Message = "Return type mismatch")]
    [DeBugInfo(49, "Nuha Ali", "10/10/2012",
         Message = "Unused variable")]
    class Rectangle
    {
        // 成员变量
        protected double length;
        protected double width;
        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }
        [DeBugInfo(55, "Zara Ali", "19/10/2012",
             Message = "Return type mismatch")]
        public double GetArea()
        {
            return length * width;
        }
        [DeBugInfo(56, "Zara Ali", "19/10/2012")]
        public void Display()
        {
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Area: {0}", GetArea());
        }
    }//end class Rectangle  

    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle(4.5, 7.5);
            r.Display();
            Type type = typeof(Rectangle);
            // 遍历 Rectangle 类的特性
            foreach (Object attributes in type.GetCustomAttributes(false))
            {
                DeBugInfo dbi = (DeBugInfo)attributes;
                if (null != dbi)
                {
                    Console.WriteLine("Bug no: {0}", dbi.BugNo);
                    Console.WriteLine("Developer: {0}", dbi.Developer);
                    Console.WriteLine("Last Reviewed: {0}",dbi.LastReview);
                    Console.WriteLine("Remarks: {0}", dbi.Message);
                }
            }

            // 遍历方法特性
            foreach (MethodInfo m in type.GetMethods())
            {
                foreach (Attribute a in m.GetCustomAttributes(true))
                {
                    DeBugInfo dbi = a as DeBugInfo;
                    if (null != dbi)
                    {
                        Console.WriteLine("Bug no: {0}, for Method: {1}",dbi.BugNo, m.Name);
                        Console.WriteLine("Developer: {0}", dbi.Developer);
                        Console.WriteLine("Last Reviewed: {0}",dbi.LastReview);
                        Console.WriteLine("Remarks: {0}", dbi.Message);
                    }
                }
            }
            Console.ReadLine();
        }
    }
}

输出

Length: 4.5
Width: 7.5
Area: 33.75
Bug no: 45
Developer: Zara Ali
Last Reviewed: 12/8/2012
Remarks: Return type mismatch

Bug no: 49
Developer: Nuha Ali
Last Reviewed: 10/10/2012
Remarks: Unused variable

Bug no: 55, for Method: GetArea
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: Return type mismatch

Bug no: 56, for Method: Display
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks:
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值