C#中的特性Attribute是一种用于为程序元素(类、方法、属性等)添加元数据信息的特殊类,这些元数据可以在编译或运行时被读取,从而实现额外的功能逻辑。特性本质上继承自System.Attribute的类的实例,通过方括号[]标注在目标元素上方使用。

C#内置特性的使用
C#已经提供了很多常用的内置特性,开发者可以直接使用这些特性来简化开发工作,下面介绍几个常见的内置特性用法。
Obsolete特性
Obsolete特性用于标记已经过时的程序元素,当其他代码调用该元素时,编译器会给出对应的警告或错误提示。
using System;
public class Demo
{
// 标记方法已过时,使用时会提示警告,第二个参数为true时会直接报错
[Obsolete("该方法已过时,请使用NewMethod代替", false)]
public void OldMethod()
{
Console.WriteLine("这是旧方法");
}
public void NewMethod()
{
Console.WriteLine("这是新方法");
}
}
Serializable特性
Serializable特性用于标记类可以被序列化,标注该特性后,类的实例可以通过序列化工具转换为字节流或字符串存储、传输。
using System;
// 标记该类支持序列化
[Serializable]
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
C#自定义特性的定义
自定义特性需要创建一个继承自System.Attribute的类,同时可以为其添加构造函数和属性来存储额外的元数据信息。定义自定义特性时,通常还会通过AttributeUsage特性来限制该特性的使用范围。
using System;
// AttributeUsage指定该特性只能用于类和方法上,AllowMultiple表示是否允许同一个元素多次使用该特性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class AuthorAttribute : Attribute
{
// 特性类的公共属性,用于存储元数据
public string Name { get; set; }
public string Version { get; set; }
// 特性的构造函数,支持位置参数
public AuthorAttribute(string name)
{
Name = name;
}
}
特性的获取与使用
特性的元数据通常在运行时通过反射获取,下面演示如何获取自定义特性和内置特性的信息。
using System;
using System.Reflection;
// 使用自定义的Author特性,位置参数对应构造函数的参数,命名参数对应属性
[Author("张三", Version = "1.0")]
[Obsolete("该类已过时")]
public class TestClass
{
[Author("李四", Version = "2.0")]
public void TestMethod() { }
}
class Program
{
static void Main()
{
Type type = typeof(TestClass);
// 获取类上的Author特性
AuthorAttribute classAuthor = type.GetCustomAttribute<AuthorAttribute>();
if (classAuthor != null)
{
Console.WriteLine($"类的作者:{classAuthor.Name},版本:{classAuthor.Version}");
}
// 获取类上的Obsolete特性
ObsoleteAttribute classObsolete = type.GetCustomAttribute<ObsoleteAttribute>();
if (classObsolete != null)
{
Console.WriteLine($"类的过时信息:{classObsolete.Message}");
}
// 获取方法上的Author特性
MethodInfo method = type.GetMethod("TestMethod");
AuthorAttribute methodAuthor = method.GetCustomAttribute<AuthorAttribute>();
if (methodAuthor != null)
{
Console.WriteLine($"方法的作者:{methodAuthor.Name},版本:{methodAuthor.Version}");
}
}
}
特性使用的注意事项
- 特性类的命名通常以Attribute结尾,使用时可以省略后缀,比如AuthorAttribute可以直接写成Author。
- 特性本身不会主动执行逻辑,需要通过反射主动读取特性元数据后才能实现对应的功能。
- 通过AttributeUsage限制特性的使用范围后,如果错误地将特性用在不符合范围的元素上,编译时会直接报错。
- 特性的构造函数参数分为位置参数和命名参数,位置参数必须对应构造函数的参数顺序,命名参数对应特性的公共属性。