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

相关推荐
fruge4 小时前
从第三方库中偷师:学习 Lodash 的函数封装技巧
学习
lingggggaaaa6 小时前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
陈天伟教授7 小时前
基于学习的人工智能(5)机器学习基本框架
人工智能·学习·机器学习
我先去打把游戏先7 小时前
ESP32学习笔记(基于IDF):基于OneNet的ESP32的OTA功能
笔记·物联网·学习·云计算·iphone·aws
初願致夕霞7 小时前
学习笔记——基础hash思想及其简单C++实现
笔记·学习·哈希算法
小女孩真可爱7 小时前
大模型学习记录(五)-------调用大模型API接口
pytorch·深度学习·学习
hd51cc7 小时前
C++ 学习笔记 名称
笔记·学习
cmcm!9 小时前
学习笔记1
数据库·笔记·学习
Hcoco_me10 小时前
YOLO目标检测学习路线图
学习·yolo·目标检测
WXG101112 小时前
【Flask】前后端交互示例
笔记·学习