C#之特性讲解

C#之特性讲解

C#中的特性(Attribute)是一种用于向程序的程序集、类型、方法、属性、参数等添加元数据的声明性标签。这些元数据可以用于描述代码的行为、功能或其他属性,并且可以在运行时通过反射来访问这些元数据。

用于在运行时传递程序中各种元素(比如类,方法,结构,枚举,组件等)的行为信息的声明标签,你可以通过使用特性向程序添加声明性消息,一个声明性标签是通过放置在他所应用的元素前面的方括号来描述的。

特性可以用于多种目的,例如:

代码注释和文档:特性可以用来提供有关代码段的额外信息,这些信息可以作为文档的一部分被提取出来。
编译器指令:某些特性可以作为编译器的指令,影响编译过程或编译生成的代码。例如,[Conditional]特性用于指定一个方法或属性仅在特定条件下编译。
运行时行为:特性可以影响代码在运行时的行为。例如,[Serializable]特性标记一个类型可以被序列化,这意味着它的实例可以被转换为字节序列以便存储或传输。
框架或库功能:一些框架或库使用特性来配置或启用特定的功能。例如,ASP.NET Web API中的[WebMethod]特性标记一个方法可以通过Web服务访问。
特性的定义包括特性名称和可选的参数列表,它们被包含在方括号[]中,并放置在要应用该特性的元素之前。

例如:

csharp
[Serializable]  
public class MyClass  
{  
    // 类的成员...  
}  
  
[Obsolete("这个方法已经过时,请使用新的方法代替。")]  
public void OldMethod()  
{  
    // 方法的实现...  
}

在上面的例子中,[Serializable]特性标记了MyClass类可以被序列化,而[Obsolete]特性则标记了OldMethod方法已经过时,并提供了替代方法的说明。

你可以创建自定义特性类,以便在你的代码中添加任何你需要的元数据信息。自定义特性类必须继承自System.Attribute类。

通过使用反射,你可以查询一个类型、方法或属性上的特性,并读取其参数值,以便在运行时动态地改变程序的行为。

元数据(清单数据)的概述:包含在程集中的元数据记录了这个程序集里有多少个 namespace、多少个类、类里有什么成员、成员的访问级别是什么……而且,元数据是以文本(也就是Unicode字符)形式存在

#define M1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AttributeTwpDemo.Method_1();

            AttributeTwpDemo.Method_2();

            AttributeTwpDemo.Method_3();

            AttributeTwpDemo.Method_4();

        }
    }
}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp12
{

    public class AttributeTwpDemo
    {
        [Conditional("M1")]
        [Conditional("M1_NoBug")]
        public static void Method_1() {

            Console.WriteLine("M1_NoBug_Method");



        }
        [Conditional("M1")]
        [Conditional("M1_Bug")]
        public static void Method_2() {
            Console.WriteLine("M1_Bug_Method");

        }
        [Conditional("M2")]
        [Conditional("M2_NoBug")]
        public static void Method_3() {
            Console.WriteLine("M2_NoBug_Method");

        }
        [Conditional("M2")]
        [Conditional("M2_Bug")]
        public static void Method_4() {
            Console.WriteLine("M2_Bug_Method");

        }

    }

    public class NoAttributeTwpDemo
    {
        public static void M1_Bug_Method() {

            Console.WriteLine("M1_Bug_Method");
        }

        public static void M1_NoBug_Method() {
            Console.WriteLine("M1_NoBug_Method");
        }

        public static void M2_Bug_Method() {
            Console.WriteLine("M2_Bug_Method");
        }

        public static void M2_NoBug_Method() {
            Console.WriteLine("M2_NoBug_Method");
        }





    }


}

在这里插入图片描述

#define M1_NoBug
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AttributeTwpDemo.Method_1();

            AttributeTwpDemo.Method_2();

            AttributeTwpDemo.Method_3();

            AttributeTwpDemo.Method_4();

        }
    }
}

在这里插入图片描述

自定义特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;

namespace WindowsFormsApp12
{

    /// <summary>
    /// 定义特性
    /// 1.是类
    /// 2.继承Attribute
    /// 3.自己的业务功能方法IsAge
    /// </summary>
    public class AttTest:Attribute
    {

        public int Age;

        public AttTest(int age)
        {
            this.Age = age;
        }

        public bool IsAge()
        {
            return this.Age >=18 ? true:false;
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TestAtt testAtt = new TestAtt();
            Type type= testAtt.GetType();
            if(type.IsDefined(typeof(AttTest),true))
            {
                var attribute = type.GetCustomAttribute<AttTest>();
                if (attribute.IsAge()){
                    testAtt.Show();
                }
            }







        }
    }


    [AttTest(20)]
    public class TestAtt
    {
        [AttTest(18)]
        public  void  Show()
        {
            Console.WriteLine("成年人!");
        }
    }
}

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值