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")]

相关推荐
red_redemption16 分钟前
自由学习记录(135)
学习
金山几座2 小时前
C#学习记录-事件
开发语言·学习·c#
X在敲AI代码2 小时前
推荐系统学习 D1推荐系统核心概述
学习·推荐算法
我的xiaodoujiao2 小时前
API接口自动化测试详细图文教程学习系列1--序章
python·学习·pytest
圆弧YH2 小时前
服务器及网站操作
学习
Alphapeople2 小时前
具身智能学习路线
学习
肖恭伟3 小时前
VScode入门学习
ide·vscode·学习
fengci.5 小时前
ctfshow(web入门)279-286
java·开发语言·学习
炽烈小老头5 小时前
【每天学习一点算法 2026/03/16】电话号码的字母组合
学习·算法
做cv的小昊6 小时前
大语言模型系统:【CMU 11-868】课程学习笔记06——Transformer学习(Transformer)
笔记·学习·语言模型