How to make .NET attribute only valid on certain types(如何使 .NET 属性仅对某些类型有效)
问题描述
可能重复:
指定 .NET 属性目标所需的基类
我想指定我的自定义属性仅对某些类型有效(基于接口或基类).
像这样:
I want to specify that my custom attribute only be valid on certain types (based on interface or base class).
Like this:
我该怎么做?
推荐答案
听起来你想创建一个 Attribute,它只能应用于某些类型,就像 AttributeUsage 只能应用于派生自 Attribute 的类型.引入这种类型的自定义限制是不可能的.您看到的特定错误不是因为自定义限制,而是简单地硬连接到编译器本身.
It sounds like you want to create an Attribute which can only be applied to certain types in much the way that AttributeUsage can only be applied to types deriving from Attribute. Introducing this type of custom restriction is simply not possible. The particular error you're seeing is not because of a custom restriction, it's one that's simply hard wired into the compiler itself.
这篇关于如何使 .NET 属性仅对某些类型有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使 .NET 属性仅对某些类型有效
基础教程推荐
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
