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 小时前
目标检测学习笔记
笔记·学习·目标检测
放下华子我只抽RuiKe55 小时前
深度学习学习路径规划
人工智能·深度学习·学习·github·ai agent·openclaw·development
今儿敲了吗5 小时前
Linux学习笔记第二章——虚拟机基础操作
linux·笔记·学习
小吴的资料屋5 小时前
经典学习项目 SSM甜品店的设计与实现
学习
huohuopro5 小时前
Hbase学习
学习·oracle·hbase
chh5635 小时前
从零开始学习C++ -- 基础知识
开发语言·c++·windows·学习·算法
NULL指向我5 小时前
STM32 F103C8T6学习笔记20:cubemx 配置 ADC采样 电位器
笔记·stm32·学习
2301_795741795 小时前
计算机网络学习笔记】初始网络之网络发展和OSI七层模型
笔记·学习·计算机网络
楼田莉子6 小时前
CMake学习:CMake在静态库工程场景上应用
开发语言·c++·后端·学习·软件构建
头疼的程序员6 小时前
计算机网络:自顶向下方法(第七版)第六章 学习分享(二)
网络·学习·计算机网络