Enum.IsDefined wykona ciąg znaków zawierający nazwę wartości wyliczeniowej. Jedynym brzydota jest to, że trzeba rozebrać wiodącą okres off File.Extension i to jest wielkość liter:
Enum.IsDefined(typeof(FileExtension), file.Extension.Substring(1).ToLower())
Edit: Rozszerzenie metoda dobroć zbliżyć się do żądanej składnię:
IEnumerable<string> GetNames(this Type t) {
if (!t.IsEnum) throw new ArgumentException();
return Enum.GetNames(t);
}
typeof(FileExtensions).GetNames().Any(e=>e.ToString().Equals(file.Extension));
Osobiście, chociaż , Wolałbym już trasę IsDefined:
bool IsDefined(this Type t, string name) {
if (!t.IsEnum) throw new ArgumentException();
return Enum.IsDefined(t, name);
}
typeof(FileExtension).IsDefined(file.Extension);
Masz rację: P –
To wymaga 'using System.Linq', poprawne? –