我应该使用什么AttributeTarget为枚举成员? [英] What AttributeTarget should I use for enum members?

查看:139
本文介绍了我应该使用什么AttributeTarget为枚举成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的 IsGPUBasedAttribute 的枚举成员是这样的:

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

什么是对使用限制于枚举成员的权利 AttributeTarget 价值?

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

编辑:带来的解释为什么从评论,枚举常量正是这样,因此他们的价值观和用法是的直接嵌入到IL 。因此枚举声明是真的不怎么从创建一个静态的类定义静态常量成员不同的:

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;        
}



...唯一的区别是它从System.Enum派生这是值类型,而不是被引用类(您不能创建一个静态的结构,也不是一个unconstructible之一)。

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

这篇关于我应该使用什么AttributeTarget为枚举成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆