I believe XSD enumerations are a more purist implementation of enumerations than .NET enumerations in the sense that they don't need and don't support numeric values associated with the enumerated names. Of course the generated code, being .NET code, will associate a numeric value with each named value internally, but this is an implementation detail that is not inherent to the nature of an enumeration as defined by the XSD standard. In this purist implementation of an enumeration, I believe the proper way to associate explicit numeric values with each enumerated name would be to define a separate collection/class that links enumerated values to numeric values. Or define additional enumerated values that represent the combined values that you support (NewInstallOrModify).
Edit:
Here's a sample of what a converter might look like.
// Generated code
public enum SetupTypeEnum
{
None,
NewInstall,
Modify,
Upgrade,
Uninstall
}
// End generated code
public struct IntMappedEnum<T> where T : struct
{
public readonly int originalValue;
public IntMappedEnum(T value)
{
originalValue = (int)Enum.ToObject(typeof(T), value);
}
public IntMappedEnum(int originalValue)
{
this.originalValue = originalValue;
}
public static implicit operator int(IntMappedEnum<T> value)
{
return 1 << value.originalValue;
}
public static implicit operator IntMappedEnum<T>(T value)
{
return new IntMappedEnum<T>(value);
}
public static implicit operator IntMappedEnum<T>(int value)
{
int log;
for (log = 0; value > 1; value >>= 1)
log++;
return new IntMappedEnum<T>(log);
}
public static explicit operator T(IntMappedEnum<T> value)
{
T result;
Enum.TryParse<T>(value.originalValue.ToString(), out result);
return result;
}
}
class Program
{
static void Main(string[] args)
{
SetupTypeEnum s = SetupTypeEnum.Uninstall;
IntMappedEnum<SetupTypeEnum> c = s;
int n = c;
IntMappedEnum<SetupTypeEnum> c1 = n;
SetupTypeEnum s1 = (SetupTypeEnum)c1;
Console.WriteLine("{0} => {1} => {2}", s, n, s1);
}
}
Edit 2:
If your enum starts at 0 (as your example does) these two changes are necessary to my example:
Updated int converter:
public static implicit operator int(IntMappedEnum<T> value)
{
return (value.originalValue == 0)?0:1 << (value.originalValue - 1);
}
The line after int log
should be:
for (log = 0; value > 0; value >>= 1)