Vma*_*man 76
你必须要爱这些人,他们认为数据不仅总是来自用户界面,而且还有你控制范围内的用户界面!
IsDefined 适用于大多数情况,你可以从:
public static bool TryParseEnum<TEnum>(this int enumValue, out TEnum retVal)
{
retVal = default(TEnum);
bool success = Enum.IsDefined(typeof(TEnum), enumValue);
if (success)
{
retVal = (TEnum)Enum.ToObject(typeof(TEnum), enumValue);
}
return success;
}
Run Code Online (Sandbox Code Playgroud)
(显然,如果你不认为它是一个合适的int扩展,只需删除'this')
dee*_*gee 21
恕我直言,标记为答案的帖子不正确.
参数和数据验证是几十年前钻进我的事情之一.
为什么
验证是必需的,因为基本上任何整数值都可以分配给枚举而不会抛出错误.
我花了很多天研究C#枚举验证,因为在许多情况下它是必要的功能.
哪里
枚举验证的主要目的是验证从文件读取的数据:您永远不知道该文件是否已损坏,或是在外部修改,还是故意被黑客攻击.
通过从剪贴板粘贴的应用程序数据的枚举验证:您永远不知道用户是否编辑了剪贴板内容.
也就是说,我花了几天时间研究和测试许多方法,包括分析我能找到或设计的每种方法的性能.
在System.Enum中调用任何东西都是如此之慢,以至于对包含数百或数千个对象的函数的性能明显下降,这些对象在其属性中有一个或多个枚举,必须对边界进行验证.
最重要的是,在验证枚举值时,远离System.Enum类中的所有内容,它非常慢.
结果
我目前用于枚举验证的方法可能会引起许多程序员的注意,但对于我的特定应用程序设计来说,它是最不邪恶的.
我定义了一个或两个常量,它们是枚举的上限和(可选)下限,并在一对if()语句中使用它们进行验证.
一个缺点是,如果更改枚举,必须确保更新常量.
此方法也只有在枚举是"自动"样式时才有效,其中每个枚举元素都是一个增量整数值,如0,1,2,3,4,....它将无法正常使用Flags或enums具有非增量值.
另请注意,如果常规int32s上的"<"">"(在我的测试中获得38,000个滴答),此方法几乎与常规方法一样快.
例如:
public const MyEnum MYENUM_MINIMUM = MyEnum.One;
public const MyEnum MYENUM_MAXIMUM = MyEnum.Four;
public enum MyEnum
{
One,
Two,
Three,
Four
};
public static MyEnum Validate(MyEnum value)
{
if (value < MYENUM_MINIMUM) { return MYENUM_MINIMUM; }
if (value > MYENUM_MAXIMUM) { return MYENUM_MAXIMUM; }
return value;
}
Run Code Online (Sandbox Code Playgroud)
性能
对于那些感兴趣的人,我在枚举验证中描述了以下变体,这里是结果.
使用随机整数输入值在每个方法的循环中以100万次循环执行分析.每次测试运行10次以上并取平均值.刻度结果包括执行的总时间,其中包括随机数生成等,但这些将在测试中保持不变.1滴= 10ns.
注意,这里的代码不是完整的测试代码,它只是基本的枚举验证方法.测试的这些还有很多其他变化,并且所有这些变化的结果与此处显示的结果相似,其中包括1,800,000个标记.
列出最慢到最快,圆润的结果,希望没有拼写错误.
在方法 = 13,600,000个刻度中确定的界限
public static T Clamp<T>(T value)
{
int minimum = Enum.GetValues(typeof(T)).GetLowerBound(0);
int maximum = Enum.GetValues(typeof(T)).GetUpperBound(0);
if (Convert.ToInt32(value) < minimum) { return (T)Enum.ToObject(typeof(T), minimum); }
if (Convert.ToInt32(value) > maximum) { return (T)Enum.ToObject(typeof(T), maximum); }
return value;
}
Run Code Online (Sandbox Code Playgroud)
Enum.IsDefined = 1,800,000 ticks
注意:此代码版本不会限制为Min/Max,但如果超出范围则返回Default.
public static T ValidateItem<T>(T eEnumItem)
{
if (Enum.IsDefined(typeof(T), eEnumItem) == true)
return eEnumItem;
else
return default(T);
}
Run Code Online (Sandbox Code Playgroud)
System.Enum使用强制转换 = 1,800,000刻度转换Int32
public static Enum Clamp(this Enum value, Enum minimum, Enum maximum)
{
if (Convert.ToInt32(value) < Convert.ToInt32(minimum)) { return minimum; }
if (Convert.ToInt32(value) > Convert.ToInt32(maximum)) { return maximum; }
return value;
}
Run Code Online (Sandbox Code Playgroud)
if()Min/Max Constants = 43,000 ticks =获胜者42x和316x更快.
public static MyEnum Clamp(MyEnum value)
{
if (value < MYENUM_MINIMUM) { return MYENUM_MINIMUM; }
if (value > MYENUM_MAXIMUM) { return MYENUM_MAXIMUM; }
return value;
}
Run Code Online (Sandbox Code Playgroud)
-eol-
Tim*_*imo 11
这是一个快速的通用解决方案,使用静态构造的HashSet<T>.
您可以在工具箱中定义一次,然后将其用于所有枚举验证。
public static class EnumHelpers
{
/// <summary>
/// Returns whether the given enum value is a defined value for its type.
/// Throws if the type parameter is not an enum type.
/// </summary>
public static bool IsDefined<T>(T enumValue)
{
if (typeof(T).BaseType != typeof(System.Enum)) throw new ArgumentException($"{nameof(T)} must be an enum type.");
return EnumValueCache<T>.DefinedValues.Contains(enumValue);
}
/// <summary>
/// Statically caches each defined value for each enum type for which this class is accessed.
/// Uses the fact that static things exist separately for each distinct type parameter.
/// </summary>
internal static class EnumValueCache<T>
{
public static HashSet<T> DefinedValues { get; }
static EnumValueCache()
{
if (typeof(T).BaseType != typeof(System.Enum)) throw new Exception($"{nameof(T)} must be an enum type.");
DefinedValues = new HashSet<T>((T[])System.Enum.GetValues(typeof(T)));
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,通过使用带有字符串键的字典(注意不区分大小写和数字字符串表示),这种方法也很容易扩展到枚举解析。
Dou*_*g S 10
正如其他人所提到的那样,Enum.IsDefined很慢,如果它处于循环中,你必须要注意的事情.
在进行多重比较时,更快的方法是首先将值放入a HashSet.然后只需Contains用来检查值是否有效,如下所示:
int userInput = 4;
// below, Enum.GetValues converts enum to array. We then convert the array to hashset.
HashSet<int> validVals = new HashSet<int>((int[])Enum.GetValues(typeof(MyEnum)));
// the following could be in a loop, or do multiple comparisons, etc.
if (validVals.Contains(userInput))
{
// is valid
}
Run Code Online (Sandbox Code Playgroud)
布拉德艾布拉姆斯Enum.IsDefined在他的文章"过度简化的危险"中特别警告.
摆脱这一要求(即,需要验证枚举)的最佳方法是删除用户可能出错的方法,例如某种输入框.例如,使用包含下拉菜单的枚举来强制仅使用有效的枚举.
这个答案是对deegee的答案的回应,它提出了System.Enum的性能问题,所以不应该把它作为我的首选通用答案,更多地解决紧密性能场景中的枚举验证问题.
如果你有一个任务关键性能问题,在一个紧凑的循环中运行缓慢但功能性的代码,那么我个人会考虑在可能的情况下将该代码移出循环,而不是通过减少功能来解决.例如,如果将来有人决定弃用某些枚举值,那么将代码限制为仅支持连续枚举可能是发现错误的噩梦.简单地说,您可以在开始时调用Enum.GetValues一次,以避免触发所有反射等数千次.这应该可以立即提升性能.如果你需要更多的表现并且你知道很多你的枚举是连续的(但你仍然想要支持'gappy'枚举)你可以更进一步,做一些事情:
public abstract class EnumValidator<TEnum> where TEnum : struct, IConvertible
{
protected static bool IsContiguous
{
get
{
int[] enumVals = Enum.GetValues(typeof(TEnum)).Cast<int>().ToArray();
int lowest = enumVals.OrderBy(i => i).First();
int highest = enumVals.OrderByDescending(i => i).First();
return !Enumerable.Range(lowest, highest).Except(enumVals).Any();
}
}
public static EnumValidator<TEnum> Create()
{
if (!typeof(TEnum).IsEnum)
{
throw new ArgumentException("Please use an enum!");
}
return IsContiguous ? (EnumValidator<TEnum>)new ContiguousEnumValidator<TEnum>() : new JumbledEnumValidator<TEnum>();
}
public abstract bool IsValid(int value);
}
public class JumbledEnumValidator<TEnum> : EnumValidator<TEnum> where TEnum : struct, IConvertible
{
private readonly int[] _values;
public JumbledEnumValidator()
{
_values = Enum.GetValues(typeof (TEnum)).Cast<int>().ToArray();
}
public override bool IsValid(int value)
{
return _values.Contains(value);
}
}
public class ContiguousEnumValidator<TEnum> : EnumValidator<TEnum> where TEnum : struct, IConvertible
{
private readonly int _highest;
private readonly int _lowest;
public ContiguousEnumValidator()
{
List<int> enumVals = Enum.GetValues(typeof (TEnum)).Cast<int>().ToList();
_lowest = enumVals.OrderBy(i => i).First();
_highest = enumVals.OrderByDescending(i => i).First();
}
public override bool IsValid(int value)
{
return value >= _lowest && value <= _highest;
}
}
Run Code Online (Sandbox Code Playgroud)
你的循环变成这样的东西:
//Pre import-loop
EnumValidator< MyEnum > enumValidator = EnumValidator< MyEnum >.Create();
while(import) //Tight RT loop.
{
bool isValid = enumValidator.IsValid(theValue);
}
Run Code Online (Sandbox Code Playgroud)
我确信EnumValidator类可以更有效地编写(这只是一个快速的黑客演示)但坦率地说谁关心导入循环之外发生了什么?唯一需要超快的位是在循环内.这就是采用抽象类路由的原因,以避免在循环中出现不必要的if-enumContiguous-then-else(工厂Create基本上是这样做的).你会注意到一些虚伪,为简洁起见,这段代码限制了int-enums的功能.我应该使用IConvertible而不是直接使用int,但这个答案已经足够冗长!
基于 Timo 的答案,这里有一个更快、更安全、更简单的解决方案,作为扩展方法提供。
public static class EnumExtensions
{
/// <summary>Whether the given value is defined on its enum type.</summary>
public static bool IsDefined<T>(this T enumValue) where T : Enum
{
return EnumValueCache<T>.DefinedValues.Contains(enumValue);
}
private static class EnumValueCache<T> where T : Enum
{
public static readonly HashSet<T> DefinedValues = new HashSet<T>((T[])Enum.GetValues(typeof(T)));
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
if (myEnumValue.IsDefined()) { ... }
Run Code Online (Sandbox Code Playgroud)
更新 - 现在 .NET 5 中的内容更加清晰:
public static class EnumExtensions
{
/// <summary>Whether the given value is defined on its enum type.</summary>
public static bool IsDefined<T>(this T enumValue) where T : struct, Enum
{
return EnumValueCache<T>.DefinedValues.Contains(enumValue);
}
private static class EnumValueCache<T> where T : struct, Enum
{
public static readonly HashSet<T> DefinedValues = new(Enum.GetValues<T>());
}
}
Run Code Online (Sandbox Code Playgroud)