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

相关推荐
喜欢踢足球的老罗5 小时前
从移动开发转型 AI Agent 工程师:我做了一个开源学习系统
人工智能·学习
wuxinyan1235 小时前
工业级大模型学习之路030:Streamlit 企业级智能体前端工作台
前端·学习·streamlit·智能体
长安紫薯6 小时前
学习AI日记
学习
星恒随风6 小时前
C语言数据结构排序算法详解(下):冒泡排序、快速排序、归并排序和计数排序
c语言·数据结构·笔记·学习·排序算法
NagatoYukee7 小时前
Spring Security基础部分学习
java·学习·spring
米小葱7 小时前
【学习笔记】cmake
笔记·学习
辰海Coding8 小时前
MiniSpring框架学习-分解 Dispatcher
java·学习·spring·架构
初夏睡觉8 小时前
数据结构学习之~二叉堆 (P3378 【模版】堆)
数据结构·c++·学习
z200509309 小时前
【Linux学习】Linux中的进程程序替换
linux·服务器·学习