仅在特定类型上允许自定义属性 [英] Allow a custom Attribute only on specific type

查看:17
本文介绍了仅在特定类型上允许自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制编译器将自定义属性的使用限制为仅用于特定的 property 类型,如 int、short、string(所有原始类型)?
类似于 AttributeUsageAttribute 的 ValidOn-AttributeTargets 枚举.

Is there a way to force the compiler to restrict the usage of a custom attribute to be used only on specific property types like int, short, string (all the primitive types)?
similar to the AttributeUsageAttribute's ValidOn-AttributeTargets enumeration.

推荐答案

不,你不能,基本上.您可以将其限制为 struct vs class vs interface,仅此而已.另外:无论如何,您都不能向代码之外的类型添加属性(通过 TypeDescriptor 除外,这不一样).

No, you can't, basically. You can limit it to struct vs class vs interface, that is about it. Plus: you can't add attributes to types outside your code anyway (except for via TypeDescriptor, which isn't the same).

这篇关于仅在特定类型上允许自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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