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

相关推荐
9ilk1 小时前
【仿RabbitMQ的发布订阅式消息队列】 ---- 功能测试联调
linux·服务器·c++·分布式·学习·rabbitmq
('-')1 小时前
《从根上理解MySQL》第一章学习笔记
笔记·学习·mysql
d111111111d1 小时前
STM32外设学习-串口发送数据-接收数据(笔记)
笔记·stm32·学习
Elias不吃糖2 小时前
eventfd 初认识Reactor/多线程服务器的关键唤醒机制
linux·服务器·c++·学习
宋辰月2 小时前
学习react第三天
前端·学习·react.js
月下倩影时3 小时前
视觉学习篇——机器学习模型评价指标
人工智能·学习·机器学习
重启编程之路3 小时前
python 基础学习socket -UDP编程
python·网络协议·学习·udp
Fantasydg3 小时前
MyBatis学习
java·学习·mybatis
song8546011344 小时前
锁的初步学习
开发语言·python·学习
AI浩5 小时前
人群计数的课程学习——是否值得:
学习