What AttributeTarget should I use for enum members?

后端 未结 4 925
时光取名叫无心
时光取名叫无心 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 08:53

    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.

提交回复
热议问题