What AttributeTarget should I use for enum members?

后端 未结 4 927
时光取名叫无心
时光取名叫无心 2021-01-01 08:42

I want to use my IsGPUBasedAttribute for enum members like this:

public enum EffectType
{
    [IsGPUBased(true)]
    PixelShader,

    [IsGPUBas         


        
4条回答
  •  情歌与酒
    2021-01-01 09:12

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

提交回复
热议问题