简体   繁体   English

将枚举值数组转换为标志枚举

[英]Converting array of enum values to flags enum

I am trying to implement functionality in a way that it was specified here:我正在尝试以此处指定的方式实现功能:

Specific solution 具体解决方案

However, I'm trying to do it as generic method to be used as an extension:但是,我正在尝试将其作为用作扩展的通用方法:

    public static TEnum? Merge<TEnum>(this IEnumerable<TEnum> values)
        where TEnum : struct, IConvertible, IComparable, IFormattable
    {
        Nullable<TEnum> merged = null;
        if (values == null || values.Count() == 0)
            return null;

        foreach(TEnum value in values)
        {
            if (merged == null)
                merged = value;
            else
            {
                merged = merged | value;
            }
        }
        return merged;
    }

Problem is however that this line:然而,问题是这一行:

merged = merged | value;

Will not compile.不会编译。 Message I'm getting is:我收到的消息是:

Operator '|'运算符“|” cannot be applied to operands of type 'TEnum?'不能应用于类型为“TEnum?”的操作数? and 'TEnum'.和'TEnum'。

Is it possible to write this generic method that will convert array of enum values to flags enum?是否可以编写这种将枚举值数组转换为标志枚举的通用方法?

There are a couple of issues here, but the biggest is that generics does not support operators - and |这里有几个问题,但最大的是泛型不支持运算符- 和| is an operator.是一个运算符。 You can hack around it via object , but then you have boxing.你可以通过object绕过它,但是你有拳击。 Here's what I would do - it generates some dynamic IL per-enum-type (once only), and uses that to do a direct "or" without boxing.这就是我要做的 - 它为每个枚举类型生成一些动态 IL(仅一次),并使用它来执行直接的“或”而无需装箱。 Note that it also uses 0 for the default return (far more expected, IMO), and avoids an explicit Count() , as that can be unpredictably expensive, and can break the enumerator (you can't guarantee that you can enumerate data more than once):请注意,它还使用0作为默认返回值(更符合预期,IMO),并避免显式Count() ,因为这可能会变得不可预测,并且可能会破坏枚举器(您不能保证您可以更多地枚举数据不止一次):

using System;
using System.Collections.Generic;
using System.Reflection.Emit;

public static class EnumUtils
{
    public static TEnum Merge<TEnum>(this IEnumerable<TEnum> values)
        where TEnum : struct
    {
        TEnum merged = default(TEnum);
        if (values != null)
        {
            var or = Operator<TEnum>.Or;
            foreach (var value in values)
            {
                merged = or(merged, value);
            }
        }
        return (TEnum)(object)merged;
    }
    static class Operator<T>
    {
        public static readonly Func<T, T, T> Or;

        static Operator()
        {
            var dn = new DynamicMethod("or", typeof(T),
                new[] { typeof(T), typeof(T) }, typeof(EnumUtils));
            var il = dn.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Or);
            il.Emit(OpCodes.Ret);
            Or = (Func<T, T, T>)dn.CreateDelegate(typeof(Func<T, T, T>));
        }

    }
}
static class Program {

    [Flags]
    public enum Foo
    {
        None = 0, A = 1,  B =2, C = 4
    }
    static unsafe void Main()
    {
        var merged = EnumUtils.Merge(new[] { Foo.A, Foo.C });

    }
}

Edit: if you really must return null for the "null or empty" case, then you could use the following tweak - but I emphasize: IMO this is an incorrect implementation - it would be more correct to simply return 0 (aka default(TEnum) ) for this scenario.编辑:如果您真的必须为“空或空”情况返回null ,那么您可以使用以下调整 - 但我强调:IMO 这是一个不正确的实现 - 简单地返回0 (又名default(TEnum)会更正确default(TEnum) ) 对于这种情况。

public static TEnum? Merge<TEnum>(this IEnumerable<TEnum> values)
    where TEnum : struct
{
    if (values == null) return null;
    using (var iter = values.GetEnumerator())
    {
        if (!iter.MoveNext()) return null;
        TEnum merged = iter.Current;
        var or = Operator<TEnum>.Or;
        while(iter.MoveNext())
        {
            merged = or(merged, iter.Current);
        }
        return merged;
    }
}

What this does is:它的作用是:

  • check for a null sequence, short-circuit检查空序列,短路
  • obtain the sequence iterator, and try to read a value - short-circuit if none获取序列迭代器,并尝试读取一个值 - 如果没有则短路
  • use the current (first) value as our seed, and obtain the operator使用当前(第一个)值作为我们的种子,并获得运算符
  • iterate the sequence, applying the operator successively迭代序列,依次应用运算符
  • return the combined value返回组合值

I needed this ability and was glad to find Marc's answer.我需要这种能力,很高兴找到 Marc 的答案。 I took a stab at using some newer language features.我尝试使用一些较新的语言功能。 Here's what I came up with:这是我想出的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

public static class Extensions
{
    /// <summary>
    /// Intended for merging an enumeration of flags enum into a single value.
    /// </summary>
    public static TEnum Merge<TEnum>(this IEnumerable<TEnum> values)
        where TEnum : struct, IConvertible
    {
        var type = typeof(TEnum);
        if (!type.IsEnum)
            throw new InvalidOperationException($"{type} is not an enum type.");

        return values.DefaultIfEmpty(default(TEnum)).Aggregate(Operator<TEnum>.Or);
    }

    static class Operator<T>
    {
        private static readonly Lazy<Func<T, T, T>> LazyOr;
        private static readonly Lazy<Func<T, T, T>> LazyAnd;

        public static Func<T, T, T> Or => LazyOr.Value;
        public static Func<T, T, T> And => LazyAnd.Value;

        static Operator()
        {
            var enumType = typeof(T);
            var underType = enumType.GetEnumUnderlyingType();
            var leftParam = Expression.Parameter(enumType, "left");
            var rightParam = Expression.Parameter(enumType, "right");
            var leftCast = Expression.ConvertChecked(leftParam, underType);
            var rightCast = Expression.ConvertChecked(rightParam, underType);

            Lazy<Func<T, T, T>> CreateLazyOp(Func<Expression, Expression, BinaryExpression> opFunc) =>
            new Lazy<Func<T, T, T>>(() =>
            {
                var op = opFunc(leftCast, rightCast);
                var resultCast = Expression.ConvertChecked(op, enumType);
                var l = Expression.Lambda<Func<T, T, T>>(resultCast, leftParam, rightParam);
                return l.Compile();
            });

            LazyOr = CreateLazyOp(Expression.Or);
            LazyAnd = CreateLazyOp(Expression.And);

        }
    }
}

1 line in Linq, no need for a helper method: Linq 中的 1 行,不需要辅助方法:

var result = enumArray.Aggregate ((TestEnum)0, (a, b) => a | b);

But if you don't want to remember this code, an extension method is also possible:但是如果你不想记住这段代码,一个扩展方法也是可以的:

private static T Merge<T> (this T[] i_enumValues)
// - or -
// private static T Merge<T> (params T[] i_enumValues)
  where T : Enum
{
  var type = typeof (T);
  if (!type.IsDefined (typeof (FlagsAttribute), false))
    throw new ArgumentException ($"The given enum type '{type}' does not have the {nameof (FlagsAttribute)}.");

  var zero = (T)Convert.ChangeType (0, Enum.GetUnderlyingType (type));

  var result = i_enumValues.Aggregate (zero, (a, b) => (dynamic)a | (dynamic)b);

  return result;
}

Proof of concept:概念证明:

[Flags]
public enum TestEnum : ulong
{
  First  = 1   << 0,
  Second = 1   << 1,
  Third  = 1   << 2,
  Fourth = 1   << 3,
  Fifth  = 1   << 4,
  Bit40  = 1ul << 40
}

private static void Main (string[] args)
{
  var enumArray = new[] { TestEnum.First, TestEnum.Second, TestEnum.Third, TestEnum.Bit40 };
  var result    = enumArray.Aggregate ((TestEnum)0, (a, b) => a | b);
  var result2   = Merge (enumArray);
}

Output:输出:

result  = First | Second | Third | Bit40
result2 = First | Second | Third | Bit40
result .ToString ("X") -> "0000010000000007"
result2.ToString ("X") -> "0000010000000007"

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

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