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

相关推荐
网络工程小王7 小时前
【HCIE-AI】10.pytorch模型迁移分析
人工智能·学习·华为·llama
魔城烟雨8 小时前
从零开始学习betaflight《3-硬件接口设计规范标准》
学习·设计规范
不言鹅喻10 小时前
HarmonyOS ArkTS 实战:实现一个单词背诵与英语学习应用
学习·华为·harmonyos
正经人_x10 小时前
学习日记42
学习
一只小菜鸡..11 小时前
南京大学 操作系统 (JYY) 学习笔记:导论与历史的轮回 (OS Introduction)
笔记·学习
小弥儿11 小时前
GitHub 今日热榜 | 2026-07-24:金融 K 线基础模型上榜
学习·金融·开源·github
520拼好饭被践踏11 小时前
JAVA+Agent学习day22
java·开发语言·后端·学习
懿路向前11 小时前
【HarmonyOS学习笔记】2026-07-24 | textProcessing 实体识别与踩坑实录
笔记·学习·边缘计算·harmonyos
菩提树下的打坐11 小时前
性能测试进阶:从 JMeter 基线到 SLO 驱动的压测体系
学习·jmeter
Albart57512 小时前
2026年Python入门路线图:从零到实战的30天逐日详解(三)
学习·flask·#python30 天入门·python 实战教程·pytest 单元测试·python 虚拟环境·logging 日志