有没有办法强制编译器来限制自定义属性的使用只在特定的属性类型如int,短,字符串(所有的原始类型)使用? 类似AttributeUsageAttribute's 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.
没有,你不能,基本上是这样。你可以把它限制在结构
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).