对于无效的泛型类型参数的最佳例外 [英] Best exception for an invalid generic type argument

查看:101
本文介绍了对于无效的泛型类型参数的最佳例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写的 UnconstrainedMelody 有泛型方法做枚举。

现在,我有一个静态类和一帮这是<方法EM>仅的意思是用标志枚举使用。我不能添加此作为约束...所以它是可能的,他们将与其他枚举类型也被调用。在这种情况下,我想抛出一个异常,但我不知道扔哪一个。



只是为了让这个具体的,如果我有这样的事情

  //返回与任何值
公共静态牛逼GetBitMask<将所有的位值; T>()其中T:(!IsFlags< T>())结构,IEnumConstraint
{
如果//此方法不抛出
{
抛出新的?
}
//正常的工作在这里
}



什么是最好例外抛出? 的ArgumentException 听起来合乎逻辑,但它是一个的键入的参数,而不是一个正常的说法,这很容易混淆的东西。我介绍一下我自己的 TypeArgumentException 类?使用出现InvalidOperationException 引发NotSupportedException ?还有别的吗?



我的,而的没有创造我自己的异常了这一点,除非它显然是做正确的事。


解决方案

引发NotSupportedException 声音的喜欢说白了适合,但文档明确地说,它应该被用于不同的目的。从MSDN类的言论:




有方法不在基类支持
,与
预期这些方法将在派生类
实现,以代替
。派生类可能
只实现从基类的方法
的一个子集,并抛出
引发NotSupportedException为
不支持的方法。




当然,有一种方法,其中引发NotSupportedException 显然是不够好,尤其是考虑到其常识性的意义。话虽如此,我不知道这是否是恰到好处。



考虑的无约束的旋律 ...




有可与通用$ b进行各种有益的事情$ b方法/类,其中有一个类型约束T:枚举或T:
代表 - 但遗憾的是,那些在C#中禁止



此实用程序库的工作原理,周围用禁令ILDASM / ILASM ...




...这似乎是一个新的例外可能是为了证明,尽管的沉重负担,我们理直气壮地有创建自定义的例外前见面。像 InvalidTypeParameterException 的东西可能是整个图书馆有用(或者也许不是 - 这无疑是一个边缘的情况下,右)。



将客户端需要能够从BCL例外区分这?当可能一个客户不小心把这个用香草枚举?你会如何​​回答的编写自定义异常类时,应考虑哪些因素?


I'm currently writing some code for UnconstrainedMelody which has generic methods to do with enums.

Now, I have a static class with a bunch of methods which are only 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.

Just to make this concrete, if I have something like this:

// 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
}

What's the best exception to throw? ArgumentException sounds logical, but it's a type argument rather than a normal argument, which could easily confuse things. Should I introduce my own TypeArgumentException class? Use InvalidOperationException? NotSupportedException? Anything else?

I'd rather not create my own exception for this unless it's clearly the right thing to do.

解决方案

NotSupportedException sounds like it plainly fits, but the documentation clearly states that it should be used for a different purpose. From the MSDN class remarks:

There are methods that are not supported in the base class, with the expectation that these methods will be implemented in the derived classes instead. The derived class might implement only a subset of the methods from the base class, and throw NotSupportedException for the unsupported methods.

Of course, there's a way in which NotSupportedException is obviously good enough, especially given its common-sense meaning. Having said that, I'm not sure if it's just right.

Given the purpose of Unconstrained Melody ...

There are various useful things that can be done with generic methods/classes where there's a type constraint of "T : enum" or "T : delegate" - but unfortunately, those are prohibited in C#.

This utility library works around the prohibitions using ildasm/ilasm ...

... it seems like a new Exception might be in order despite the high burden of proof we justly have to meet before creating custom Exceptions. Something like InvalidTypeParameterException might be useful throughout the library (or maybe not - this is surely an edge case, right?).

Will clients need to be able to distinguish this from BCL Exceptions? When might a client accidentally call this using a vanilla enum? How would you answer the questions posed by the accepted answer to What factors should be taken into consideration when writing a custom exception class?

这篇关于对于无效的泛型类型参数的最佳例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆