我目前正在为 无拘无束的旋律编写一些代码,这些代码使用了与枚举有关的通用方法。
Now, I have a static class with a bunch of methods which are 只有 meant to be used with "flags" enums. I can't add this as a constraint... so it's possible that they'll be called with other enum types too. In that case I'd like to throw an exception, but I'm not sure which one to throw.
具体来说,如果我有这样的东西:
// Returns a value with all bits set by any values
public static T GetBitMask<T>() where T : struct, IEnumConstraint
{
if (!IsFlags<T>()) // This method doesn't throw
{
throw new ???
}
// Normal work here
}
最好的例外是什么?ArgumentException
听起来合乎逻辑,但它是一个 类型参数,而不是一个正常的参数,这很容易混淆事物。我应该介绍我自己的 TypeArgumentException
类吗?使用 InvalidOperationException
?NotSupportedException
?还有别的事吗?
我不会为此创建我自己的异常,除非这显然是正确的事情。