Considerate ho questo metodo di estensione:controllo se un enum ha qualche bandiere in comune
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;
}
E la seguente situazione:
[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
voglio verificare se foo ha qualche bandiere in comune con barra, ma ci deve essere un modo migliore per ottenere questo comportamento nel metodo di estensione.
Ho anche provato come questo, ma è restituisce sempre vero:
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;
}
Grazie, molto bello! – Erpel