C# 特性
特性
自定义特性类
特性是一种比较特殊的类,作为代码对象附加部分用于向运行时提供一些补充信息。
主要有三个特征:
- 从Attribute类派生
- 名称一般以Attribute结尾
- 声明特性类时必须在类上加AttributeUsage,用于标注当前声明的类应用于哪些对象
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)] public class MyDemoAttribute : Attribute { public string Description { get; set; } } public class OrderData { [MyDemo(Description = "订单 ID")] public int OrdID { get; set; } [MyDemo(Description = "添加时间")] public DateTime AddTime { get; set; } [MyDemo(Description = "计算折扣价")] public double Compute(double q) { return q * 0.98d; } }
向特性类构造函数传递参数
特性类若只有无参构造函数,应用时可以忽略小括号。若有有参构造函数,那么应在小括号中填上参数。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class DoubleRangeAttribute : Attribute { public double Largest { get; } public DoubleRangeAttribute(double largest) { Largest = largest; } } [DoubleRange(700d)] public class Test { }
同一对象上应用多个特性
声明特性类时,AttributeUsage
类的AllowMultiple
属性用于设置是否允许在同一对象上应用多个特性实例,默认false
。
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class CustomAttribute : Attribute { public string Ver { get; set; } } [Custom(Ver = "1.0.0.1"), Custom(Ver = "1.0.2.0")] public class AppData { }
运行阶段对数据验证
声明一个特性类
[AttributeUsage(AttributeTargets.Property)] public class MyAttribute : Attribute { public char StartChar { get; set; } public int MaxLen { get; set; } }
把MyAttribute特性应用到测试类的RawName属性上,StartChar表示以“k”开头,MaxLen表示最大长度为7.
public class Test { [My(StartChar = 'k', MaxLen = 7)] public string RawName { get; set; } }
需要用到反射,通过Type类得到对象的各种信息,通过GetCustomAttribute
扩展方法得到已应用的特性实例。
class Program { static void Main(string[] args) { Test v = new Test { RawName = "k003d6ex915f" }; // 调用方法进行属性值验证 bool b = CheckTest(v, nameof(Test.RawName)); if (b) Console.WriteLine("验证通过。"); else Console.WriteLine("验证失败。"); Console.Read(); } static bool CheckTest(Test t, string property) { // 获取类型信息 Type type = t.GetType(); // 查找属性成员 PropertyInfo prop = type.GetProperty(property, BindingFlags.Public | BindingFlags.Instance); if (prop == null) { return false; } // 获取特性 MyAttribute att = prop.GetCustomAttribute<MyAttribute>(); // 获取实例的属性值 string value = prop.GetValue(t) as string; if (string.IsNullOrEmpty(value)) return false; // 进行验证 if (value.StartsWith(att.StartChar) == false) return false; if (value.Length > att.MaxLen) return false; return true; } }
返回值上应用特性
[AttributeUsage(AttributeTargets.ReturnValue)] public class CheckSumAttribute : Attribute { }
[return: CheckSum] static string SaySomething() => "Hello";
本文来自博客园,作者:一纸年华,转载请注明原文链接:https://www.cnblogs.com/nullcodeworld/p/15351341.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)