Generic SetFlag / UnsetFlag-method - 检查wheter int是无符号的

时间:2015-07-20 09:24:28

标签: c# vb.net enums

我编写了一个自定义扩展方法来设置/取消设置Enum-Flag,它基于一些StackOverflow答案。代码基本上如下所示:

  Public Sub SetFlag(Of T As {Structure})(ByRef storage As T, value As T)
            EnsureTypeIsEnum(Of T)()
            Dim underlyingType As Type = System.Enum.GetUnderlyingType(storage.GetType())

            If (underlyingType Is GetType(UInt64)) Then
                Dim this = Convert.ToUInt64(storage)
                Dim flag = Convert.ToUInt64(value)

                storage = DirectCast(System.Enum.ToObject(GetType(T), (this Or flag)), T)
            Else
                Dim this = Convert.ToInt64(storage)
                Dim flag = Convert.ToInt64(value)
                Dim result = DirectCast((this Or flag), Object)

                storage = DirectCast(System.Enum.ToObject(GetType(T), (this Or flag)), T)
            End If
        End Sub

我不确定是否必须检查值是已签名还是未签名。如果没有为枚举指定某种类型,则默认为已签名。有没有什么好的理由将Enum指定为无符号整数?

我试着通过查看.NET源代码得到答案。 Enum.HasFlag方法不执行此检查。它总是将值转换为ulong。我无法想象这样做是否安全。有任何陷阱吗?

 public Boolean HasFlag(Enum flag) {
            if (!this.GetType().IsEquivalentTo(flag.GetType())) {
                throw new ArgumentException(Environment.GetResourceString("Argument_EnumTypeDoesNotMatch", flag.GetType(), this.GetType())); 
            }

            ulong uFlag = ToUInt64(flag.GetValue()); 
            ulong uThis = ToUInt64(GetValue());
            return ((uThis & uFlag) == uFlag); 
        }

更新

我发现Enum-class将所有值静默转换为UInt64。它还可以非常好地转换负数而不会抛出OverflowException,从而生成完全预期的值。

internal static ulong ToUInt64(Object value) 
        {
            // Helper function to silently convert the value to UInt64 from the other base types for enum without throwing an exception.
            // This is need since the Convert functions do overflow checks.
            TypeCode typeCode = Convert.GetTypeCode(value); 
            ulong result;

            switch(typeCode) 
            {
                case TypeCode.SByte: 
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    result = (UInt64)Convert.ToInt64(value, CultureInfo.InvariantCulture); 
                    break;

                case TypeCode.Byte: 
                case TypeCode.UInt16:
                case TypeCode.UInt32: 
                case TypeCode.UInt64:
                    result = Convert.ToUInt64(value, CultureInfo.InvariantCulture);
                    break;

                default:
                // All unsigned types will be directly cast 
                    Contract.Assert(false, "Invalid Object type in ToUInt64"); 
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
            } 
            return result;
        }

1 个答案:

答案 0 :(得分:1)

Enum Test As ULong
    Zero
    One
    Alot = &H8000000000000000UL
End Enum

这是麻烦制造者的一个例子。如果您不特意处理UInt64,那么当您尝试时,SetFlag()将使用OverflowException进行炸弹:

Dim v As Test
SetFlag(v, Test.Alot)

Convert.ToInt64()对大于Int64.MaxValue的值不满意。

仅使用ToUint64()不起作用:

Enum Test
    MinusOne = -1
    Zero
    One
End Enum

炸弹:

Dim v As Test
SetFlag(v, Test.MinusOne)

所以不,你不能在不破坏它的情况下简化这段代码。