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

相关推荐
重启编程之路33 分钟前
python 基础学习socket -TCP编程
网络·python·学习·tcp/ip
石像鬼₧魂石40 分钟前
Kali Linux 中对某(靶机)监控设备进行漏洞验证的完整流程(卧室监控学习)
linux·运维·学习
d111111111d1 小时前
STM32通信协议学习--I2C通信(了解)
笔记·stm32·单片机·嵌入式硬件·学习
盼哥PyAI实验室2 小时前
学会给网页穿衣服——学习 CSS 语言
前端·css·学习
我的xiaodoujiao2 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 25--数据驱动--参数化处理 Excel 文件 2
前端·python·学习·测试工具·ui·pytest
Rousson3 小时前
硬件学习笔记--89 MCU主频对Uart波特率影响及采样点
笔记·单片机·学习
Fantasydg3 小时前
AJAX JSON学习
前端·学习·ajax
越努力越幸运5083 小时前
AJAX 学习第一天:axios、HTTP 基础、serialize 插件
学习·http·ajax
im_AMBER3 小时前
HTTP 02 会话 | 消息 | MIME类型
网络·笔记·网络协议·学习·http
lijun_xiao20094 小时前
Apache Hadoop-学习笔记1
hadoop·学习·apache