通用SetFlag/UnsetFlag方法-检查int是否未签名
本文关键字:是否 int 检查 SetFlag UnsetFlag 方法 通用 | 更新日期: 2023-09-27 18:03:27
我已经编写了一个用于设置/取消设置枚举标志的自定义扩展方法,该方法基于一些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
我不确定是否必须检查该值是有符号的还是无符号的。如果未为枚举指定特定类型,则默认情况下会对其进行签名。是否有充分的理由将枚举指定为无符号整数?
我试图通过查看.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类静默地将所有值转换为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;
}
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)
所以不,你不能在不破坏它的情况下简化这个代码。