问题描述
有没有办法强制编译器将自定义属性的使用限制为仅用于特定的 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).
这篇关于仅在特定类型上允许自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!