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

相关推荐
小陈phd11 分钟前
多模态大模型学习笔记(二十三)——一文搞懂数虚拟人:从定义、分类到核心技术全景
笔记·学习
糖果店的幽灵11 分钟前
【大模型】大模型学习总结之机器学习-3.模型评估
人工智能·学习·机器学习
Edward1111111126 分钟前
3月23Math类,Arrays类
java·学习
码农42736 分钟前
点评项目深入改造-------日常学习笔记
java·笔记·学习·搜索引擎·全文检索
云边散步1 小时前
godot2D游戏教程系列二(19)
笔记·学习·游戏·游戏开发
我的xiaodoujiao2 小时前
4、API 接口自动化测试详细图文教程学习系列4--相关Python基础知识3
python·学习·测试工具·pytest
学机械的鱼鱼2 小时前
【学习笔记】如何快速理解ROS2
笔记·学习
red_redemption2 小时前
自由学习记录(145)
学习
醇氧2 小时前
【学习】封锁协议
网络·学习·oracle
前端小趴菜~时倾2 小时前
自我提升-python爬虫学习:day03
爬虫·python·学习