C#自学笔记:特性

特性

概念

特性是一种允许我们向程序的程序集添加元数据的语言结构

它是用于保护程序结构信息的某种特殊类型的类


特性提供功能强大的方法以将声明信息与 c# 代码(类型、方法、属性等)相关联

特性与程序实体关联后,即可在运行时使用反射查询特性信息


特性的目的是告诉编译器把程序结构的某组元数据嵌入程序集中

它可以放置在几乎所有的声明中(类、变量、函数等等声明)


说人话:

  • 特性本质是个类
  • 我们可以利用特性类为元数据添加额外信息
  • 比如一个类、成员变量、成员方法等等为他们添加更多的额外信息
  • 之后可以通过反射来获取这些额外信息

自定义特性

继承特性基类
 class MyCustomAttribute : Attribute
 {
     //特性中的成员,一般根据需求来写
     public string info;

     public MyCustomAttribute(string info)
     {
         this.info = info;
     }

     public void TestFun()
     {
         Console.WriteLine("特性的方法");
     }
 }

基本语法

[特性名(参数列表)]

本质上就是在调用特性类的构造函数

写在哪里?

  • 类、函数、变量上一行,表示他们具有该特性信息
[MyCustom("这是我自己写的一个用于计算的类")]//
class MyClass
{
    [MyCustom("这是一个成员变量")]
    public int value;

    [MyCustom("这是一个用于计算加法的函数")]
    public void TestFun([MyCustom("函数参数")]int a)
    {
    }
}

使用

static void Main(string[] args)
{
    MyClass mc = new MyClass;
    Type t = mc.GetType();

    //参数一:特性的类型
    //参数二:代表是否搜索继承链,意思就是是否向父类寻找该特性(属性和事件忽略此参数)
    if (t.isDefined(typeof(MyCustomAttribute), false)
    {
        Console.WriteLine("该类型应用了MyCustom特性");
    }

    //获取Type元数据中的所有特性
    object[] array = t.GetCustomAttributes(true);
    for (int i = 0; i < array.Length; i++)
    {
        if (array[i] is MyCustomAttribute)
        {
            Console.WriteLine((array[i] as MyCustomAttribute).info);
            (array[i] as MyCustomAttribute).TestFun();
        }
    }
}     

限制自定义特性的使用范围

通过为特性类加特性限制其使用范围
//参数一:AttributeTargets - 特性能够用在哪些地方
//参数二:AllowMultiple - 是否允许多个特性实例用在同一个目标上
//参数三:Inherited - 里面的特性是否能被派生类和重写成员继承
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true, Inherited = true]
public class MyCustomAttribute : Attribute
{
}

系统自带特性—过时特性

过时特性:Obsolete

用于提示用户,使用的方法等成员已经过时,建议使用新方法

一般加在函数前的特性

class TestClass
{
    //参数一:调用过时方法时,提示的内容
    //参数二:true-使用该方法时会报错,false-使用该方法时直接警告
    [Obsolete("OldSpeak方法已经过时了,请使用Speak方法", false)]
    public void OldSpeak(string str)
    {
        Console.WriteLine(str);
    }
    public void Speak()
    {
    }
}

系统自带特性—调用者信息特性

  1. CallerFilePath 特性
    1. 哪个文件调用
  2. CallerLineNumber 特性
    1. 哪一行调用
  3. CallerMemberName 特性
    1. 哪个函数调用

需要引用命名空间 using System.Runtime.CompilerServices;

一般作为函数参数的特性

class TestClass
{
    public void SpeakCaller(string str, [CallerFilePath]string fileName = "",
                            [CallerLineNumber]int line = 0, [CallerMemberName]string target = "")
    {
        Console.WriteLine(str);			//123123
        Console.WriteLine(fileName);	//C:\Users\MECHREVO\Desktop\CSharp进阶教学\Lesson21_特性\Program.cs
        Console.WriteLine(line);		//18
        Console.WriteLine(target);		//Main
    }
}

class Program
{
    static void Main(string[] args)
    {
        TestClass tc = new TestClass();
        tc.SpeakCaller("123123");
    }
}

一般要用都是用在try catch 的 catch 里面,异常捕获的时候可以打印出错误信息

系统自带特性—条件编译特性

条件编译特性:Conditional

它会和预处理指令 #define 配合使用

需要引用命名空间 using system.diagnostics;

  • 主要可以用在一些调试代码上
  • 有时想执行有时不想执行的代码

要先用预处理指令定义参数里的符号,函数才会被编译进去,才会被执行。

#define Fun
class Program
{
    [Conditional("Fun")]
    static void Fun()
    {
        Console.WriteLine("Fun执行");
    }

    static void Main(string[] args)
    {
        Fun();
    }
}

系统自带特性—外部Dll包函数特性

外部Dll包函数特性:DllImport

用来标记非 .Net(C#) 的函数,表明该函数在一个外部的DLL中定义

一般用来调用 C 或者 C++ 的Dll包写好的方法

需要引用命名空间 using System.Runtime.InteropServices

[DllImport("Test.dll")]
public static extern void Add(int a, int b);

相当于把dll包中的相同方法映射到C#里面,后续调用该方法相当于使用dll包里写好的方法。

posted @ 2025-08-17 23:22  柠凉w  阅读(13)  评论(0)    收藏  举报