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

相关推荐
日拱一卒的小田1 小时前
ZYNQ学习笔记4-ZYNQ的SD卡控制器1
笔记·学习
拼图2094 小时前
Git常用语法
分布式·git·学习
Vcaker5 小时前
Linux学习29-kubernetes网络
学习
dadaobusi8 小时前
学习:奈奎斯特
学习
传奇开心果编程10 小时前
【Go入门练中学】第2课:条件与循环
后端·学习·golang
小小de风呀10 小时前
de风——【从零开始学C++】(二十)红黑树封装map和set
c++·学习
爱吃苹果的日记本10 小时前
数据结构第四课—线性表Linear List
数据结构·学习
昌原的儿子LEO10 小时前
ARM 汇编核心知识点总结
嵌入式硬件·学习
Ray Wang11 小时前
Context上下文工程
python·学习·ai编程
百里香酚兰11 小时前
【Unity学习笔记】如何把粉色Prefab还原
笔记·学习·unity