60

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?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • Now it's possible to use it (from Net5 on) https://learn.microsoft.com/en-us/dotnet/api/system.attributetargets?view=net-5.0 – Tena Nov 11 '22 at 11:14

4 Answers4

74

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:

public static class MyEnum
{
    public const int Value1 = 0;
    public const int Value2 = 1;
    public const int Value3 = 2;
    public const int Value4 = 3;        
}

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

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • Thanks that works. Do you know why MS didn't provide one for enum constants? – Joan Venge Feb 17 '11 at 18:12
  • Because at the compiled level, there's not a lot of difference between an enum constant and an ordinary member field; you'd have to examine the containing type and determine it derives from Enum. Also, there really isn't a lot of call for an attribute that can only be applied to enum constants. The only one I've ever applied to an enum constant is DescriptionAttribute, from System.ComponentModel, which can be applied to anything AFAIK. – KeithS Feb 17 '11 at 18:25
  • Thanks, I also only used DescriptionAttribute for enum constants, this is a similar attribute that I am using in the actual code. – Joan Venge Feb 17 '11 at 18:32
  • You say there isn't much difference, but the biggest difference that `enum`s being first class citizens make is that they give you a strongly typed way of handling it. In your case I can do this `if (MyEnum.Value1 == 235)` or `int i = MyEnum.Value1; i = SomeOtherEnum.Value1` etc.. but not possible with enums (without some explicit casting or so). – nawfal Jun 09 '13 at 06:59
  • I know that this post is really old, but my current case is to randomly select enum values based on an entry value, to make certain options more likely to occur and to exclude options that cannot be randomized, `public enum Test { [DisallowRandom]None,[Entry(4)]Test1,[Entry(2)]Test2,[Entry(1)]Test3}` –  Sep 12 '20 at 20:49
33

AttributeTargets.Field allow you to use attribute for enum values.

[AttributeUsage(AttributeTargets.Field)]
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
4

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 (or EffectType) class and implementing these as ordinary properties if you're going to have multiple attributes like this.

For example,

public class EffectType
{
    public bool IsGpuBased { get; private set; }

    private EffectType(bool isGpuBased)
    {
        IsGpuBased = isGpuBased;
    }

    public static readonly EffectType PixelShader = new EffectType(true);
    public static readonly EffectType Blur = new EffectType(false);
}

Taking this approach will give you code that's both easier to read and will perform better compared to metadata extraction.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 1
    Thanks, the actual code isn't like this, just wanted to give a simplified hypothetical example for the question. – Joan Venge Feb 17 '11 at 18:25
  • @Joan: I think It'd still stand by my recommendation. – Adam Robinson Feb 17 '11 at 18:26
  • 1
    That's ok, I am just saying the code I posted is made up, because I only want to know the restriction of attributes to enum constants. – Joan Venge Feb 17 '11 at 18:31
  • @Joan: On that question, I'd second @KeithS's comment in that it's likely not there because there isn't enough demand. And I'd still say that a custom type is a better option compared to using attributes ;) – Adam Robinson Feb 17 '11 at 18:44
1
[AttributeUsage(AttributeTargets.Field)]
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
jsDevia
  • 1,282
  • 4
  • 14
  • 37