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

相关推荐
艾莉丝努力练剑13 分钟前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全
蒸蒸yyyyzwd1 小时前
数据库相关错题笔记
笔记·学习
蒸蒸yyyyzwd3 小时前
后端学习笔记,设计模式相关以及微服务相关
笔记·学习
Hhy_11073 小时前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio
艾莉丝努力练剑4 小时前
【QT】系统相关:TCP的API介绍
开发语言·前端·c++·人工智能·qt·学习·tcp/ip
yiangL_17 小时前
Python 学习笔记:递归、turtle绘图、字符串与函数
笔记·python·学习
优化Henry20 小时前
LTE 站点 S1 链路反复闪断导致小区频闪退服故障排查
运维·网络·学习·信息与通信·tdd
β添砖java1 天前
Transformer大白话学习笔记
笔记·学习·transformer
kevin_org1 天前
LangChain入门学习(四)
python·学习·langchain·fastapi
瑞克-桑切斯2 天前
Kubernetes 1.28 保姆级新手学习教程
运维·学习·docker·云原生·容器·kubernetes