C# 使用接口的隐式运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/143485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:26:50  来源:igfitidea点击:

implicit operator using interfaces

提问by Michael Meadows

I have a generic class that I'm trying to implement implicit type casting for. While it mostly works, it won't work for interface casting. Upon further investigation, I found that there is a compiler error: "User-defined conversion from interface" that applies. While I understand that this should be enforced in some cases, what I'm trying to do does seem like a legitimate case.

我有一个通用类,我正在尝试为其实现隐式类型转换。虽然它主要工作,但它不适用于界面转换。经过进一步调查,我发现有一个编译器错误:“用户定义的接口转换”适用。虽然我知道这应该在某些情况下强制执行,但我正在尝试做的事情似乎是合法的。

Here's an example:

下面是一个例子:

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);
    }
}

Code to use it:

使用它的代码:

var concreteReferenceToBar = new ConcreteBar();
IBar intefaceReferenceToBar = concreteReferenceToBar;
Foo<ConcreteBar> concreteFooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromInterfaceBar = intefaceReferenceToBar; // doesn't work

Does anyone know a workaround, or can anyone explain in a satisfactory way why I shuouldn't be able to cast interfaceReferenceToBarimplicitly to Foo<IBar>, since in my case it is not being converted, but only contained within Foo?

有谁知道解决方法,或者谁能以令人满意的方式解释为什么我不能interfaceReferenceToBar隐式转换为Foo<IBar>,因为在我的情况下它没有被转换,而只包含在 Foo 中?

EDIT:It looks like covariance might offer salvation. Let's hope the C# 4.0 specification allows for implicit casting of interface types using covariance.

编辑:看起来协方差可能会带来救赎。让我们希望 C# 4.0 规范允许使用协方差隐式转换接口类型。

采纳答案by Adam Hughes

The reason you can't do this is because it is specifically forbidden in the C# language specification:

你不能这样做的原因是因为它在 C# 语言规范中是明确禁止的:

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 objector an interface-type.

如果满足以下所有条件,则允许类或结构声明从源类型 S 到目标类型 T 的转换:

  • ...
  • S 和 T 都不是object接口类型

and

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-typesucceeds only if the object being converted actually implements the specified interface-type.

不允许用户定义的转换从或转换为 interface-types。特别是,此限制确保在转换为interface-type时不会发生用户定义的转换,并且仅当被转换的对象实际实现了指定的 interface-type 时,才能成功转换为 interface-type

Source

来源