将泛型类型参数显式转换为任何接口
在泛型常见问题解答:最佳实践中说:
编译器将允许您显式强制转换任何接口的通用类型参数,但不是类:
interface ISomeInterface
{...}
class SomeClass
{...}
class MyClass<T>
{
void SomeMethod(T t)
{
ISomeInterface obj1 = (ISomeInterface)t;//Compiles
SomeClass obj2 = (SomeClass)t; //Does not compile
}
}
我认为类和接口的限制都是合理的,除非类/接口未指定为约束类型。
那么为什么会有这样的行为,为什么接口允许这样的行为呢?
In Generics FAQ: Best Practices says :
The compiler will let you explicitly cast generic type parameters to any interface, but not to a class:
interface ISomeInterface
{...}
class SomeClass
{...}
class MyClass<T>
{
void SomeMethod(T t)
{
ISomeInterface obj1 = (ISomeInterface)t;//Compiles
SomeClass obj2 = (SomeClass)t; //Does not compile
}
}
I see limitation reasonable for both, classes and interfaces, unless the class/interface is not specified as constraint type.
So why such behavior, why it is allowed for interfaces ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这是因为转换为
SomeClass
可能意味着任意数量的事情,具体取决于可用的转换,而转换为ISomeInterface
只能是引用转换或装箱转换。选项:
首先转换为对象:
使用
as
代替:显然,在第二种情况下,您还需要随后执行无效检查,以防
t
不是SomeClass
。编辑:C# 4 规范的第 6.2.7 节给出了这样做的原因:
I believe this is because the cast to
SomeClass
can mean any number of things depending on what conversions are available, whereas the cast toISomeInterface
can only be a reference conversion or a boxing conversion.Options:
Cast to object first:
Use
as
instead:Obviously in the second case you would also need to perform a nullity check afterwards in case
t
is not aSomeClass
.EDIT: The reasoning for this is given in section 6.2.7 of the C# 4 specification:
在C#的继承原理中,接口可以被继承多次,而类只能被继承一次。
由于接口的继承具有复杂的层次结构,
.net框架不需要在编译时确保泛型类型T是特定接口。(编辑)
相反,可以通过在编译时声明类型约束来确保类是特定的类,如以下代码所示。
In the inheritance principle of C#, interfaces could be inherited multiple times, but a class just once.
As the inheritance from interfaces has complex hierarchy,
the .net framework does not need to ensure the generic type T a specific interface at the compile time.(EDIT)
On the contrary, a class could be ensured a specific class with declaring a type constraint at the compile as the following code.