gdoron gdoron - 4 months ago 49
C# Question

Allow a custom Attribute only on specific type

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.


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).