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

相关推荐
你在我身后29 分钟前
发展路线建议
学习
彧azz32 分钟前
数据结构:关于图的学习
c语言·数据结构·笔记·学习·算法
苦猿的大模型日记37 分钟前
Day57|从0学习 Claude Code(七):我没把说明书全塞给它,用到哪本才翻哪本
学习
GHL2842710901 小时前
豆包以图生图学习
学习·ai
小诗懂技术1 小时前
【硬件学习入门】--嵌入式51单片机点灯与延迟效应
学习
DevangLic2 小时前
opencode go要完蛋了吗
运维·学习
小弥儿2 小时前
GitHub今日热榜 | 2026-09-02:i-have-adhd 登顶
学习·开源
Yanjun2i2 小时前
Agent学习记录四:多工具时如何处理
人工智能·python·学习·agent
传奇开心果编程3 小时前
【Xilem 0.4 基础语法学与练】第二课视图修饰符、列表渲染、组件拆分、memoize 性能优化
学习·rust·前端框架