繁体   English   中英

C#:Enum.IsDefined 在组合标志上

[英]C#: Enum.IsDefined on combined flags

我有这个枚举:

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

我试图在这个(或任何,真的)枚举上做一个包装器,它会通知更改。 目前它看起来像这样:

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

由于枚举确实可以分配任何值,因此我想检查是否定义了给定的值。 但是我发现了一个问题。 如果我在这里给它一个枚举,例如Csv | Excel Csv | Excel ,然后Enum.IsDefined将返回false 显然是因为我没有定义任何由这两个组成的枚举。 我想这在某种程度上是合乎逻辑的,但是我应该如何检查给定的值是否有效? 换句话说,为了让它工作,我需要用什么来交换下面的行?

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

我们知道转换为字符串的枚举值永远不会以数字开头,但具有无效值的数字总是会。 这是最简单的解决方案:

public static bool IsDefinedEx(this Enum yourEnum)
{
    char firstDigit = yourEnum.ToString()[0];
    if (Char.IsDigit(firstDigit) || firstDigit == '-')  // Account for signed enums too..
        return false;

    return true;
}

使用该扩展方法而不是股票 IsDefined 应该可以解决您的问题。

对于基于标志的枚举,关键在于是否设置了位。 因此,对于“ExportFormat”,如果设置了第 1 位,则它是 CSV 格式,即使可能设置了更多位。 位 1 和位 2 是否设置了无效值? 这是主观的:从作为一个组的值的角度来看,它是无效的(没有为第 1 位和第 2 位集定义位模式)但是,因为每个值都是一个位,单独查看它们,可能是位 1 和 2 设置的值有效。

如果传入值 0011111011,这是一个有效值吗? 嗯,这取决于您要查找的内容:如果您查看的是整个值,那么它是一个无效值,但是如果您查看的是单个位,则它是一个不错的值:它设置了一些位,但不是已定义,但没关系,因为基于标志的枚举是“按位”检查的:您不是将它们与值进行比较,而是检查是否设置了位。

因此,由于您的逻辑将检查设置了哪些位以选择要选择的格式,因此实际上没有必要检查是否定义了枚举值:您有 3 种格式:如果设置了相应格式的位,则格式为被选中。 这就是你应该写的逻辑。

我会在位级别进行操作并检查新值中设置的所有位是否都设置在您的All值中:

if ( ! (All & NewValue) == NewValue )

您将不得不看看自己如何最好地做到这一点,也许您需要将所有值转换为 int 然后进行按位比较。

[Flags] enum E { None = 0, A = '1', B = '2', C = '4' }

public static bool IsDefined<T>(T value) where T : Enum
{
    var values = Enum.GetValues(typeof(T)).OfType<dynamic>().Aggregate((e1, e2) => (e1 | e2));

    return (values & value) == value;
}

// IsDefined(ExportFormat.Csv); // => True
// IsDefined(ExportFormat.All); // => True
// IsDefined(ExportFormat.All | ExportFormat.None); // => True
// IsDefined(ExportFormat.All | ExportFormat.Csv); // => True
// IsDefined((ExportFormat)16); // => False
// IsDefined((ExportFormat)int.MaxValue); // => False

// IsDefined(E.A); // => True
// IsDefined(E.A | E.B); // => True
// IsDefined((E)('1' | '2')); // => True
// IsDefined((E)('5')); // => True
// IsDefined((E)5); // => True
// IsDefined((E)8); // => False
// IsDefined((E)int.MaxValue); // => False

是一个有效的小扩展方法。

static void Main(string[] args)
{
  var x = ExportFormat.Csv | ExportFormat.Excel;
  var y = ExportFormat.Csv | ExportFormat.Word;
  var z = (ExportFormat)16; //undefined value

  var xx = x.IsDefined();  //true
  var yy = y.IsDefined();  //false
  var zz = z.IsDefined();  //false
}

public static bool IsDefined(this Enum value)
{
  if (value == null) return false;
  foreach (Enum item in Enum.GetValues(value.GetType()))
    if (item.HasFlag(value)) return true;
  return false;
}

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

以下方法适用于未在枚举中分组的由代码组合的项目:

static void Main(string[] args)
{
  var x = ExportFormat.Csv | ExportFormat.Excel;
  var y = ExportFormat.Csv | ExportFormat.Word;
  var z = (ExportFormat)16; //undefined value

  var xx = x.IsDefined();  //true
  var yy = y.IsDefined();  //true
  var zz = z.IsDefined();  //false
}

public static bool IsDefined(this ExportFormat value)
{
  var max = Enum.GetValues(typeof(ExportFormat)).Cast<ExportFormat>()
    .Aggregate((e1,e2) =>  e1 | e2);
  return (max & value) == value;
}

如果您使用的是支持 DLR 的 C# 4.0,您可以使用以下很酷的不可知扩展方法:

public static bool IsDefined(this Enum value)
{
  dynamic dyn = value;
  var max = Enum.GetValues(value.GetType()).Cast<dynamic>().
    Aggregate((e1,e2) =>  e1 | e2);
  return (max & dyn) == dyn;
}

注意 - 必须这样做,因为:

  1. 运营商| &不能应用于EnumEnum类型的操作数
  2. 这些运算符是在编译器中定义的,不会被反射,所以没有办法用反射/Linq 表达式来检索它们,相信我 - 我已经试过了......

这是一种方法(使用 Linq):

    private static bool IsDefined<T>(long value) where T : struct
    {
        var max = Enum.GetValues(typeof(T)).Cast<T>()
            .Select(v => Convert.ToInt64(v)).
            Aggregate((e1, e2) => e1 | e2);
        return (max & value) == value;
    }

也许尝试用解析捕捉?
您不想传递哪些值?

    public T Value
    {
        get { return value; }
        set
        {
            try
            {
                Enum.Parse(typeof(T), value.ToString());
            }
            catch 
            {
                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"));
            }
        }
    }

看看Enums.NET库的IsValid方法:

var validExportFormat = ExportFormat.Excel | ExportFormat.Csv;
validExportFormat.IsValid(EnumValidation.IsValidFlagCombination); // => true

var invalidExportFormat = (ExportFormat)100;
invalidExportFormat.IsValid(EnumValidation.IsValidFlagCombination); // => false

枚举值的任何有效组合都会产生一个非数字值:

public static class EnumExtensions
{
    public static bool IsDefined(this Enum value) => !ulong.TryParse(value.ToString(), out _);
}

我知道这个帖子已经有一段时间没有人回答了,但我认为使用内置函数回答它对那些在我之后访问这个帖子的人有好处。

使用 OP 的原始枚举,您可以使用以下代码解析位掩码值。

    ExportFormat format;
    if (!Enum.TryParse<ExportFormat>(value.ToString(), out format))
    {
      // Could not parse
    }

希望有帮助。

这里 相当多的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM