我有一个通用类,我正在尝试为其实现隐式类型转换。虽然它大部分都有效,但它不适用于界面转换。经过进一步调查,我发现存在编译器错误:适用的“用户定义的接口(interface)转换”。虽然我知道这在某些情况下应该强制执行,但我正在尝试做的事情看起来确实像是一个合法的案例。
这是一个例子:
public class Foo<T> where T : IBar
{
private readonly T instance;
public Foo(T instance)
{
this.instance = instance;
}
public T Instance
{
get { return instance; }
}
public static implicit operator Foo<T>(T instance)
{
return new Foo<T>(instance);
}
}
使用代码:
var concreteReferenceToBar = new ConcreteBar();
IBar intefaceReferenceToBar = concreteReferenceToBar;
Foo<ConcreteBar> concreteFooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromInterfaceBar = intefaceReferenceToBar; // doesn't work
有谁知道解决方法,或者谁能以令人满意的方式解释为什么我不能投 interfaceReferenceToBar
隐含到 Foo<IBar>
,因为在我的例子中它没有被转换,而是只包含在 Foo 中?
编辑: 看起来协方差可能会提供救赎。我们希望 C# 4.0 规范允许使用协变隐式转换接口(interface)类型。
最佳答案
你不能这样做的原因是因为它在 C# 语言规范中被明确禁止:
Source: ECMA-334 Section 15.10.4
A class or struct is permitted to declare a conversion from a source type S to a target type T provided all of the following are true:
- ...
- Neither S nor T is
object
or an interface-type.
和
User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type.
关于c# - 使用接口(interface)的隐式运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/143485/