gdoron gdoron - 1 year ago 108
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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download