I have an XML that contains several flags, some of them are unsigned 32-bit integers and others are unsigned 64-bit integers. Some of them are written in a comma-separated list and others are in hex style.
See this example:
<Color>Blue,Red</Color>
<Color>0xC</Color>
As I don't want to write a method to parse each enum, I decided to use a generic method. But Visual Studio won't let me build the solution. Here's my method:
public static T ParseFlags<T>(string value) where T : struct
{
T result = (T)((object)0);
string[] array;
// Remove white spaces and delimit string if it is comma-separated
if (ParseDelimitedString(value, ',', out array))
{
for (int i = 0; i < array.Length; i++)
{
T flag = (T)((object)0);
// Check if value is member of enumeration
if (Enum.TryParse<T>(array[i], out flag))
{
result |= (T)((object)flag);
}
}
}
else
{
switch (Type.GetTypeCode(Enum.GetUnderlyingType(typeof(T))))
{
// Remove hex characters and parse node's inner text
case TypeCode.UInt32:
result = (T)((object)ParseUint(value));
break;
case TypeCode.UInt64:
result = (T)((object)ParseUlong(value));
break;
}
}
return result;
}
The error message I get is:
Error 1 Operator '|=' cannot be applied to operands of type 'T' and 'T'
Is there a way to do this?