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

相关推荐
redaijufeng2 分钟前
网络爬虫学习:应用selenium获取Edge浏览器版本号,自动下载对应版本msedgedriver,确保Edge浏览器顺利打开。
爬虫·学习·selenium
腾科IT教育13 分钟前
零基础快速上岸HCIP,高效学习思路分享
学习·华为认证·hcip·hcip考试·hcip认证
234710212714 分钟前
4.14 学习笔记
笔记·python·学习
醇氧16 分钟前
【学习】软件过程模型全解析:从瀑布到敏捷的演进之路
学习·log4j
邪修king1 小时前
UE5 零基础入门第三弹: 碰撞与触发交互,解锁场景机关与蓝图封装(高娱乐性学习)
学习·ue5·交互
今天你TLE了吗2 小时前
LLM到Agent&RAG——AI概念概述 第二章:提示词
人工智能·笔记·后端·学习
烤麻辣烫2 小时前
JS基础
开发语言·前端·javascript·学习
red_redemption2 小时前
自由学习记录(168)
学习·已经运行中世界-模与约束·闭环
2601_954971133 小时前
2026数学专业,靠什么技能能发挥数理优势转数据岗?
学习
xuanwenchao3 小时前
ROS2学习笔记 - 2、类的继承及使用
服务器·笔记·学习