I have an enum, suppose it is called sports:
enum Sports
{
Baseball = 1,
Basketball = 2,
Football = 4,
Hockey = 8,
Soccer = 16,
Tennis = 32,
etc...
}
I would basically like to add an extension method that clears a mask bit like this:
Sports Mask = Sports.Baseball | Sports.Football | Sports.Tennis; //37
Mask.Clear(Sports.Baseball);
// Mask = Football | Tennis
This is the extension method I've come up with, it doesn't work. Clear doesn't affect Mask, the value remains 37. I'm not sure I can modify the this portion from within the extension method. Is there some other way to accomplish this?
public static void Clear<T>(this Enum value, T remove)
{
Type type = value.GetType();
object result;
if (type.Equals(typeof(ulong)))
{
result = Convert.ToUInt64(value) & ~Convert.ToUInt64((object)remove);
}
else
{
result = Convert.ToInt64(value) & ~Convert.ToInt64((object)remove);
}
value = (Enum)Enum.Parse(type, result.ToString());
}