如何使.NET属性仅适用于特定类型的 [英] How to make .NET attribute only valid on certain types

查看:264
本文介绍了如何使.NET属性仅适用于特定类型的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/1190649/specify-required-base-class-for-net-attribute-targets">Specify对于.NET属性目标所需的基类

我希望指定我的自定义属性仅是对某些类型的(基于接口或基类)有效。
像这样的:

I want to specify that my custom attribute only be valid on certain types (based on interface or base class).
Like this:

我怎么能这样做?

推荐答案

这听起来像你想创建一个属性这只能适用于特定类型的多少方式 AttributeUsage 只能应用于类型从属性导出。引入这种类型的自定义限制是根本不可能的。你所看到的特定的错误是不是因为一个自定义的限制,这是一个根本就硬连接到编译器本身。

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属性仅适用于特定类型的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆