C# 特性 学习理解记录

在类或函数上一行xxx, 查看源码是 xxxAttribute

比如 NUnit 的单元测试 Test特性,

public class TestAttribute : NUnitAttribute, ISimpleTestBuilder, IApplyToTest, IImplyFixture,

可以在vs里测试》测试资源管理器》选中标注特性的函数进行单独测试

比如 System.ComponentModel.Description("描述类、函数的特性")

属于 public class DescriptionAttribute : Attribute

-也可以使用System.ComponentModel.DisplayName("123")

AttributeUsage(AttributeTargets.Class \| AttributeTargets.Method \| AttributeTargets.Property \| AttributeTargets.Event)

public class DisplayNameAttribute : Attribute

AttributeTargets.Class 这些事标识范围,DisplayName可以标识类,函数,属性,事件

场景:

函数返回的是英文,但特性标注了中文,可以用反射获取特性的描述,然后显示中英文;

System.ComponentModel.Description("猫")

public string getTheClass()

{

return cat;

}

自定义特性 :xxxAttribute 并继承atribute

标注:MyDescription(Name="商品名称3")

相关推荐
小弥儿41 分钟前
GitHub今日热榜 | 2026-09-15:AI 智能体与本地生成工具领跑
学习·开源
susplus1 小时前
【初识ARM】ARM相关基础概念
学习·arm
XiHongShi20161 小时前
键盘大小写指示器(代替键盘灯)
学习·计算机
斯维赤3 小时前
AI Agent学习之路 | Prompt Engineering(提示词工程)的三板斧核心技巧
人工智能·学习·prompt
2301_802651085 小时前
imx6ull裸机开发:UART
学习
辣知5 小时前
辣知·化智56 良渚神权与夏朝颠覆
学习
dadaobusi6 小时前
学习:三层交换机
学习
笨鸟先飞的橘猫6 小时前
树结构在游戏行业中的应用
学习·游戏
yangmu32037 小时前
从显像管到Micro-LED:屏幕的光之进化
学习·显示器
半生过往8 小时前
前端工程师学习智能体开发(一)
前端·学习·状态模式