news 2026/2/2 11:51:45

特性与反射总结

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
特性与反射总结

一、概念对比

特性(Attributes)

是什么:给代码元素贴的"标签"或"注解"
作用:为代码添加额外信息(元数据)
时机:编译时和运行时都可以读取

反射(Reflection)

是什么:在运行时"查看"和"操作"代码的能力
作用:动态获取类型信息、创建对象、调用方法
时机:只能在运行时使用

二、关系:特性 + 反射 = 强大功能

工作流程:

text

1. 用特性给代码"贴标签"(编译时) 2. 用反射读取这些"标签"(运行时) 3. 根据标签信息执行相应操作

三、基本使用格式

1. 定义自定义特性

csharp

// 1. 继承Attribute类 // 2. 使用AttributeUsage指定应用范围 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class CustomAttribute : Attribute { // 属性 public string Description { get; set; } public int Version { get; set; } // 构造方法 public CustomAttribute(string description) { Description = description; Version = 1; } }

2. 使用特性

csharp

[Custom("学生类", Version = 2)] public class Student { [Custom("姓名属性")] public string Name { get; set; } [Custom("学习方法")] public void Study() { } }

3. 用反射读取特性

csharp

// 获取类型信息 Type type = typeof(Student); // 读取类上的特性 CustomAttribute classAttr = (CustomAttribute)type.GetCustomAttribute(typeof(CustomAttribute)); if (classAttr != null) { Console.WriteLine($"类描述: {classAttr.Description}"); Console.WriteLine($"版本: {classAttr.Version}"); } // 读取属性上的特性 foreach (var property in type.GetProperties()) { var attr = property.GetCustomAttribute<CustomAttribute>(); if (attr != null) { Console.WriteLine($"属性 {property.Name}: {attr.Description}"); } } // 读取方法上的特性 foreach (var method in type.GetMethods()) { var attr = method.GetCustomAttribute<CustomAttribute>(); if (attr != null) { Console.WriteLine($"方法 {method.Name}: {attr.Description}"); } }

四、AttributeUsage参数详解

1. AttributeTargets(可以贴在哪里)

csharp

// 单个目标 AttributeTargets.Class // 类 AttributeTargets.Method // 方法 AttributeTargets.Property // 属性 AttributeTargets.Field // 字段 AttributeTargets.Constructor // 构造方法 AttributeTargets.Struct // 结构体 AttributeTargets.Interface // 接口 AttributeTargets.Enum // 枚举 AttributeTargets.Parameter // 参数 AttributeTargets.Assembly // 程序集 // 多个目标(用 | 连接) AttributeTargets.Class | AttributeTargets.Method // 所有成员 AttributeTargets.All

2. AllowMultiple(是否可以贴多个)

csharp

AllowMultiple = false // 只能贴一个(默认) AllowMultiple = true // 可以贴多个 // 示例: [MyAttr("标签1")] [MyAttr("标签2")] // 只有当AllowMultiple=true时才允许 public class MyClass { }

3. Inherited(是否被子类继承)

csharp

Inherited = true // 子类可以继承特性(默认) Inherited = false // 子类不继承特性

五、反射常用方法

1. 获取类型信息

csharp

// 三种方式获取Type对象 Type type1 = typeof(Student); // 编译时已知类型 Type type2 = obj.GetType(); // 运行时对象 Type type3 = Type.GetType("命名空间.类名"); // 通过字符串 // 常用属性和方法 type.Name // 类名 type.FullName // 完整名称(含命名空间) type.Namespace // 命名空间 type.BaseType // 父类类型 type.IsClass // 是否是类 type.IsPublic // 是否是public

2. 获取成员信息

csharp

// 获取所有方法 MethodInfo[] methods = type.GetMethods(); // 获取所有属性 PropertyInfo[] properties = type.GetProperties(); // 获取所有字段 FieldInfo[] fields = type.GetFields(); // 获取所有构造方法 ConstructorInfo[] constructors = type.GetConstructors();

3. 动态创建对象和调用方法

csharp

// 1. 创建对象 object obj = Activator.CreateInstance(type); // 2. 设置属性值 PropertyInfo prop = type.GetProperty("Name"); prop.SetValue(obj, "张三"); // 3. 获取属性值 string name = (string)prop.GetValue(obj); // 4. 调用方法 MethodInfo method = type.GetMethod("Study"); method.Invoke(obj, null); // 无参数方法 // 5. 调用带参数的方法 MethodInfo method2 = type.GetMethod("SetAge"); method2.Invoke(obj, new object[] { 20 });

六、完整示例:数据验证系统

1. 定义验证特性

csharp

[AttributeUsage(AttributeTargets.Property)] public class ValidationAttribute : Attribute { public string ErrorMessage { get; set; } public bool Required { get; set; } public int MinLength { get; set; } = 0; public int MaxLength { get; set; } = int.MaxValue; }

2. 使用特性标记模型

csharp

public class Student { [Validation(Required = true, MinLength = 2, ErrorMessage = "姓名不能为空且至少2个字符")] public string Name { get; set; } [Validation(Required = true, ErrorMessage = "年龄必须填写")] public int Age { get; set; } [Validation(Required = true, MaxLength = 11, ErrorMessage = "手机号格式不正确")] public string Phone { get; set; } }

3. 反射读取并验证

csharp

public class Validator { public static List<string> Validate(object obj) { List<string> errors = new List<string>(); Type type = obj.GetType(); foreach (PropertyInfo property in type.GetProperties()) { // 获取特性 ValidationAttribute attr = property.GetCustomAttribute<ValidationAttribute>(); if (attr != null) { // 获取属性值 object value = property.GetValue(obj); // 验证必填 if (attr.Required && (value == null || string.IsNullOrEmpty(value.ToString()))) { errors.Add(attr.ErrorMessage); continue; } // 验证长度 if (value != null) { string strValue = value.ToString(); if (strValue.Length < attr.MinLength || strValue.Length > attr.MaxLength) { errors.Add(attr.ErrorMessage); } } } } return errors; } }

4. 使用验证器

csharp

Student student = new Student { Name = "张", // 长度不够 Age = 0, Phone = "123456789012" // 长度超过 }; var errors = Validator.Validate(student); foreach (var error in errors) { Console.WriteLine(error); }

七、总结表格

方面特性(Attribute)反射(Reflection)
目的添加元数据读取和操作元数据
时机编译时定义运行时使用
使用方式用 [] 声明用 Type 类操作
常见用途配置、验证、序列化动态加载、插件系统
性能几乎没有影响较慢,避免频繁使用

八、最佳实践

1. 特性使用原则

csharp

// 好的做法 [AttributeUsage(AttributeTargets.Property)] // 明确目标 public class MyAttribute : Attribute { public string Description { get; } // 只读属性 public MyAttribute(string description) // 构造方法 { Description = description; } } // 使用 [My("这是一个重要的属性")] public string ImportantProperty { get; set; }

2. 反射使用原则

csharp

// 缓存Type信息(提高性能) private static readonly Type _cachedType = typeof(MyClass); // 使用泛型减少类型转换 public T CreateInstance<T>() where T : new() { return new T(); // 比反射创建快 } // 必要时才用反射 if (needDynamic) { // 使用反射 } else { // 使用静态代码 }

九、一句话总结

特性是贴标签,反射是读标签,两者结合实现动态编程。

text

工作流程: 1. 设计特性 → 定义标签格式 2. 标注代码 → 贴上标签 3. 反射读取 → 运行时查看标签 4. 执行逻辑 → 根据标签决定行为
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/29 20:39:28

手把手教你用8款免费AI神器,从零到一轻松搞定毕业论文

面对毕业论文这座大山&#xff0c;你是否感到无从下手&#xff1f;从选题、找文献、搭框架到撰写、修改、降重&#xff0c;每一步都让无数学生感到焦虑。别担心&#xff0c;AI时代已经到来&#xff0c;借助强大的AI工具&#xff0c;你完全可以更高效、更轻松地完成这项任务。 …

作者头像 李华
网站建设 2026/1/30 15:13:27

大数据实战:如何构建高效的大数据处理平台?

大数据实战&#xff1a;如何构建高效的大数据处理平台&#xff1f;关键词&#xff1a;大数据处理平台、高效构建、数据存储、数据处理、数据应用 摘要&#xff1a;本文围绕如何构建高效的大数据处理平台展开&#xff0c;从背景知识入手&#xff0c;详细解释大数据处理平台相关核…

作者头像 李华
网站建设 2026/2/2 4:43:36

从零开始学LuatOS:深入理解系统消息列表机制!

LuatOS以简洁高效著称&#xff0c;其核心之一便是基于消息驱动的系统架构。系统消息列表作为事件传递的载体&#xff0c;连接了硬件层与应用层。许多新手在开发中遇到逻辑阻塞或事件丢失问题&#xff0c;往往源于对消息列表机制理解不足。本文将带你从零开始&#xff0c;层层剖…

作者头像 李华
网站建设 2026/2/1 16:32:52

18. CPU给GPU传输数据

1.CPU给GPU传输数据1.CPU给GPU传输数据 1).直接传输的原始数据("CPU传实际字节到GPU显存")这类是GPU渲染必须的"原材料", CPU会把数据打包成连续缓冲区上传到GPU显存a.顶点属性比如: 顶点坐标, 法线, 切线, UV坐标, 顶点颜色, 骨骼权重(蒙皮动画用), GPU会…

作者头像 李华