Consider I have this extension method:
public static bool HasAnyFlagInCommon(this System.Enum type, Enum value)
{
var flags = value.ToString().Split(new string[] { ", " },
StringSplitOptions.None);
foreach (var flag in flags)
{
if (type.ToString() == flag)
return true;
}
return false;
}
And the following situation:
[Flags]
enum Bla
{
A = 0,
B = 1,
C = 2,
D = 4
}
Bla foo = Bla.A | Bla.B;
Bla bar = Bla.A;
bar.HasAnyFlagInCommon(foo); //returns true
I want to check if foo has any flags in common with bar, but there got to be a better way of achiving this behavior in the extension method.
I also tried like this, but is always returns true:
public static bool HasAnyFlagInCommon(this System.Enum type, Enum value)
{
var flags = Enum.GetValues(value.GetType()).Cast<Enum>()
.Where(item => value.HasFlag(item));
foreach (var flag in flags)
{
if (type == flag)
return true;
}
return false;
}