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

相关推荐
抠脚学代码1 小时前
Ubuntu18.6 学习QT问题记录以及虚拟机安装Ubuntu后的设置
qt·学习·ubuntu
萌新小码农‍8 小时前
Spring框架学习day7--SpringWeb学习(概念与搭建配置)
学习·spring·状态模式
蓝婷儿8 小时前
6个月Python学习计划 Day 15 - 函数式编程、高阶函数、生成器/迭代器
开发语言·python·学习
行云流水剑8 小时前
【学习记录】深入解析 AI 交互中的五大核心概念:Prompt、Agent、MCP、Function Calling 与 Tools
人工智能·学习·交互
一弓虽8 小时前
zookeeper 学习
分布式·学习·zookeeper
苗老大8 小时前
MMRL: Multi-Modal Representation Learning for Vision-Language Models(多模态表示学习)
人工智能·学习·语言模型
xhyu619 小时前
【学习笔记】On the Biology of a Large Language Model
笔记·学习·语言模型
小白杨树树9 小时前
【SSM】SpringMVC学习笔记7:前后端数据传输协议和异常处理
笔记·学习
dddaidai12313 小时前
kafka入门学习
分布式·学习·kafka