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

相关推荐
2501_943695338 分钟前
大专市场调查与统计分析专业,怎么学习市场调研问卷的设计?
人工智能·学习
sponge'13 分钟前
opencv学习笔记14:transformer
笔记·学习·transformer
xhyu611 小时前
【学习笔记】推荐系统 (4.召回:Deep Retrieval、其他召回通道、曝光过滤)
笔记·学习
小帅学编程2 小时前
Python学习
开发语言·python·学习
CappuccinoRose2 小时前
HTML语法学习文档 - 汇总篇
前端·学习·html5
Aliex_git3 小时前
Sentry 私有部署和配置笔记
笔记·学习·sentry
四谎真好看3 小时前
SSM学习笔记(SpringBoot篇)
spring boot·笔记·学习·学习笔记·ssm
学编程的闹钟3 小时前
E语言子程序调用详解
学习
郝学胜-神的一滴4 小时前
Effective Modern C++ 条款40:深入理解 Atomic 与 Volatile 的多线程语义
开发语言·c++·学习·算法·设计模式·架构
追光少年33224 小时前
React学习:ES6
学习·react.js·es6