I have written a custom extension method for setting/unsetting an Enum-Flag, which is based on some StackOverflow answers. The code basically looks as follows:
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
I am unsure whether I have to check if the value is signed or unsigned. When not specifying a certain type for an Enum, it is per default signed. Is there any good reason to specify an Enum as unsigned integer?
I tried to get an answer by looking at the .NET source code. The Enum.HasFlag-method does not perform this check. It always casts the value to ulong. I can't imagine that it is secure to do that. Are there any pitfalls?
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);
}
UPDATE:
I found out that the Enum-class silently converts all values to an UInt64. It also converts negative numbers very well without throwing an OverflowException, thus generating exactly the expected value.
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;
}