Is it possible to write the following function as a template where
"EnumFlags" becomes template parameter T (where T will always be an
enumerator with the "FlagsAttribute"). Nothing I've tried works, including
use of a "where" clause since T must always be of type "System.Enum". Thanks
in advance.
void SetFlag(EnumFlags Flag, ref EnumFlags Flags, bool On)
{
if (On)
{
Flags |= Flag;
}
else
{
Flags &= ~Flag;
}
}
Sorry,
Marc
Thanks for the confirmation. I didn't think so but casting is an ugly option
I wanted to avoid (notwithstanding the type issue). I'll think it's cleaner
to just replicate the same function over and over again as required (given
that I have little choice). Anyway, thanks again.
static T SwapEnumFlag<T>(int flags, int flag) where T : struct
{
Debug.Assert(typeof(T).IsEnum, "T must be an enum");
Debug.Assert(typeof(T).IsDefined(typeof(FlagsAttribute), false), "T
must have the System.FlagsAttribute applied to it.");
if ((flags & flag) == flag)
flags &= ~flag;
else
flags |= flag;
return (T)Enum.ToObject(typeof(T), flags);
}
FlaggedEnum flagged = FlaggedEnum.Red | FlaggedEnum.Blue;
Debug.WriteLine(flagged); // output: Red, Blue
flagged = SwapEnumFlag<FlaggedEnum>((int)flagged,
(int)FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Blue
flagged = SwapEnumFlag<FlaggedEnum>((int)flagged,
(int)FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Red, Blue
Thanks very much. Though I think casting is ugly and I normally try to avoid
it, I'll take a look at this in greater detail. I might use it as is or
leverage it in some way.Your effort is certainly appreciated. Thanks again.