G
Guest
Due to the poor performance Enum.IsDefined producesi’m looking for an
alternative to this utility method:
Note: the enums passed to the method will not change during runtime.
public static object EnumTranslate(Type enumType, object enumValue) {
if (!Enum.IsDefined(enumType, enumValue)) {
if (Attribute.IsDefined(enumType, typeof(FlagsAttribute))) {
// just ignore
} else {
throw new InvalidEnumArgumentException();
}
}
return Enum.ToObject(enumType, enumValue);
}
alternative to this utility method:
Note: the enums passed to the method will not change during runtime.
public static object EnumTranslate(Type enumType, object enumValue) {
if (!Enum.IsDefined(enumType, enumValue)) {
if (Attribute.IsDefined(enumType, typeof(FlagsAttribute))) {
// just ignore
} else {
throw new InvalidEnumArgumentException();
}
}
return Enum.ToObject(enumType, enumValue);
}