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

相关推荐
自由日记20 分钟前
css学习9
前端·css·学习
Larry_Yanan1 小时前
QML学习笔记(五十二)QML与C++交互:数据转换——时间和日期
开发语言·c++·笔记·qt·学习·ui·交互
TL滕1 小时前
从0开始学算法——第二天(时间、空间复杂度)
数据结构·笔记·学习·算法
kblj55552 小时前
学习Linux——网络基础管理
linux·网络·学习
天狗下凡2 小时前
【OpenGL学习】第1课: 概念与流程理解
学习
黑岚樱梦2 小时前
计算机网络第六章学习
学习·计算机网络
c0d1ng2 小时前
自建督学习——BERT(第二十二周周报)
人工智能·学习·bert
木霖森772 小时前
软件工程入门学习
学习·软件工程
BullSmall2 小时前
《道德经》第五十章
学习
Larry_Yanan3 小时前
QML学习笔记(五十三)QML与C++交互:数据转换——序列类型与 JavaScript 数组的转换
c++·笔记·学习