C#:Enum.IsDefined上结合标志 [英] C#: Enum.IsDefined on combined flags

查看:1126
本文介绍了C#:Enum.IsDefined上结合标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的枚举:

  [国旗] 
公共枚举ExportFormat
{
无= 0,
的CSV = 1,
TSV = 2,
的Excel = 4,
=所有的Excel | CSV | TSV
}



我想就这一个包装(或任何,真的)枚举该通知上的变化。目前,它看起来像这样:

 公共类NotifyingEnum< T> :INotifyPropertyChanged的
,其中T:结构
{
私人的T值;

公共事件PropertyChangedEventHandler的PropertyChanged;

公共NotifyingEnum()
{
如果
抛出新的ArgumentException(T型必须是一个枚举)(typeof运算(T).IsEnum!);
}

公众吨价
{
{返回值; }

{
如果(!Enum.IsDefined(typeof运算(T),值))
抛出新ArgumentOutOfRangeException(价值,价值的价值枚举没有定义+ typeof运算(T).Name点); (!this.value.Equals(值))

如果
{
THIS.VALUE =价值;

PropertyChangedEventHandler处理器=的PropertyChanged;
如果(处理!= NULL)
处理器(这一点,新PropertyChangedEventArgs(值));
}
}
}
}



由于一个枚举可以用任何的价值真正被分配,我要检查,如果给定值被定义。但是,我发现了一个问题。如果我在这里给它由例如的CSV枚举| Excel中,那么 Enum.IsDefined 将返回。这显然是因为我还没有确定由这两个中的任何枚举。我想这在一定程度上是合理的,但我应该怎么然后检查是否给定的值是有效的?换句话说,使其工作,需要什么交换这下面符合?



 如果(!Enum.IsDefined (typeof运算(T),值))


解决方案

通过标志基于枚举,它是关于有一点一套与否。所以关于'ExportFormat',如果位1被置位,它的CSV格式,即使可能有更多的位置。是具有1位和2所设置的值无效?这是主观:从视图中的值作为一个组的点,它是无效的(不存在于第1位和2界定位模式),然而,因为每一个值是一个位,看他们个别地,它可以是一个与位1和2的设定值是有效的。



如果一个在价值0011111011通行证,是一个有效的价值?嗯,这取决于你在寻找什么:如果你正在寻找在整个价值,那么它是一个无效的值,但如果你正在寻找在各个位,这是一个确定的值:它已设置位不能定义的,但没关系,基于旗枚举的每比特检查:你不比较他们的值,你检查了一下是否设置与否。



所以,你的逻辑将检查哪些位设置为选择哪些格式来接,这是真的不需要检查枚举值是否定义:你有3个格式:若对应的格式的位被置位,该格式被选中。这就是你应该写的逻辑。


I have this enum:

[Flags]
public enum ExportFormat
{
    None = 0,
    Csv = 1,
    Tsv = 2,
    Excel = 4,
    All = Excel | Csv | Tsv
}

I am trying to make a wrapper on this (or any, really) enum which notifies on change. Currently it looks like this:

public class NotifyingEnum<T> : INotifyPropertyChanged
    where T : struct
{
    private T value;

    public event PropertyChangedEventHandler PropertyChanged;

    public NotifyingEnum()
    {
        if (!typeof (T).IsEnum)
            throw new ArgumentException("Type T must be an Enum");
    }

    public T Value
    {
        get { return value; }
        set
        {
            if (!Enum.IsDefined(typeof (T), value))
                throw new ArgumentOutOfRangeException("value", value, "Value not defined in enum, " + typeof (T).Name);

            if (!this.value.Equals(value))
            {
                this.value = value;

                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs("Value"));
            }
        }
    }
}

Since an enum can be assigned with any value really, I want to check if the given Value is defined. But I found a problem. If I here give it an enum consisting of for example Csv | Excel, then Enum.IsDefined will return false. Apparently because I haven't defined any enum consisting of those two. I guess that on some level is logical, but how should I then check if the given value is valid? In other words, to make it work, what do I need to swap this following line with?

if (!Enum.IsDefined(typeof (T), value))

解决方案

With flag-based enums, it's about having a bit set or not. So for 'ExportFormat', if bit 1 is set, it's CSV format, even though there might be more bits set. Is having bit 1 and 2 set an invalid value? This is subjective: from the point of view of the values as a group, it is invalid (there's no bitpattern defined for bits 1 and 2 set) however, as each value is a bit, looking at them individually, it can be that a value with bits 1 and 2 set is valid.

If one passes in the value 0011111011, is that a valid value? Well, it depends on what you're looking for: if you are looking at the whole value, then it's an invalid value, but if you're looking at individual bits, it's an ok value: it has bits set which aren't defined, but that's ok, as flag-based enums are checked 'per bit': you're not comparing them to a value, you're checking whether a bit is set or not.

So, as your logic will check on which bits are set to select which formats to pick, it's realy not necessary to check whether the enum value is defined: you have 3 formats: if the bit of the corresponding format is set, the format is selected. That's the logic you should write.

这篇关于C#:Enum.IsDefined上结合标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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