I want to use my IsGPUBasedAttribute
for enum members like this:
public enum EffectType
{
[IsGPUBased(true)]
PixelShader,
[IsGPUBased(false)]
Blur
}
but the compiler doesn't let me to use:
[AttributeUsage (AttributeTargets.Enum, AllowMultiple = false)]
What is the right AttributeTarget
value to limit the usage to enum members?
Far as I know, there isn't one specifically for enum constants. The closest you could get would probably be "Field", which limits the use to field members of a class or struct (which Enum constants are treated as for purposes of attributes).
EDIT: bringing the explanation of "why" up from the comments, Enum constants are exactly that, and as such their values and usages are embedded directly into the IL. An enum declaration is therefore really not very different from creating a static class definition with static constant members:
... the only difference being that it derives from System.Enum which is a value type instead of being a reference class (you can't create a static struct, nor an unconstructible one).
AttributeTargets.Field allow you to use attribute for enum values.
There isn't a way to specify that an attribute can be used only on enum members. Honestly, you're probably better off creating your own
Effect
(orEffectType
) class and implementing these as ordinary properties if you're going to have multiple attributes like this.For example,
Taking this approach will give you code that's both easier to read and will perform better compared to metadata extraction.