(四十)C#编程基础复习——C#特性(Attribute)

特性是一种用于程序运行时传递各种元素(例如类、方法、结构、枚举等)行为信息的声明性代码。使用特性可以将元数据(例如编译器指令、注释、描述、方法和类等信息)添加到程序中。

在C#中,特性具有以下属性:

  1. 使用特性可以向程序中添加元数据,元数据是指程序中各种元素的相关信息,所有.NET程序中都包含一组指定的元数据;
  2. 可以将一个或多个特性应用于整个程序、模块或者较小的程序元素(例如类和属性)中;
  3. 特性可以像方法和属性一样接收自变量
  4. 程序可使用反射来检查自己的元数据或其他程序中的元数据。

一、定义特性

C#中定义特性的语法格式如下:

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

其中,[]中用来定义特性的名称和值,positionnal_parameters用来指定基本信息,name_parameter用来指定可选信息。

二、预定义特性

.Net Frameword中提供了三个预定义的属性:

  1. AttributeUsage;
  2. Conditional;
  3. Obsolete.

namespace _042
{
    internal class Program
    {
        static void function1()
        {
            Myclass.Message("Function1函数");
            function2();
        }
        static void function2()
        {
            Myclass.Message("Function2函数");
        }
        static void Main(string[] args)
        {
            Myclass.Message("Main函数");
            function1();
            Console.ReadKey();
        }
    }
    public class Myclass
    {
        [Conditional ("DEBUG")]
        public static void Message(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}


namespace _043
{
    internal class Program
    {
        [Obsolete("OldMethod已弃用,请改用NewMethod", true)]
        static void OldMethod()
        {
            Console.WriteLine("已弃用的函数");
        }
        static void NewMethod()
        {
            Console.WriteLine("新定义的函数");
        }
        static void Main(string[] args)
        {
            //OldMethod();
            NewMethod();
            Console.ReadKey();
        }
    }
}

三、自定义特性

// 一个自定义特性 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;
      }
  }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace c.biancheng.net
{
    class Demo
    {
        static void Main(string[] args)
        {
            Rectangle rec = new Rectangle(12, 15);
            rec.Display();
            Console.ReadKey();
        }
    }

    [DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "返回值类型不匹配")]
    [DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "未使用变量")]
    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 = "返回值类型不匹配")]
        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());
        }
    }
    // 一个自定义特性 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;
            }
        }
    }
}

 

posted @   代号六零一  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示