只允许自定义属性的特定类型 [英] Allow a custom Attribute only on specific type
问题描述
有没有办法强制编译器来限制自定义属性的使用只在特定的属性类型如int,短,字符串(所有的原始类型)使用?
类似<一href="http://msdn.microsoft.com/en-us/library/system.attributeusageattribute%28v=vs.95%29.aspx">AttributeUsageAttribute's ValidOn- <一href="http://msdn.microsoft.com/en-us/library/system.attributetargets%28v=vs.95%29.aspx">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.
推荐答案
没有,你不能,基本上是这样。你可以把它限制在结构
VS 类
VS 接口
,也差不多了。另外:你不能外的code(通过 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屋!