gpt4 book ai didi

c# - 使用接口(interface)的隐式运算符

转载 作者:IT王子 更新时间:2023-10-29 03:48:33 27 4
gpt4 key购买 nike

我有一个通用类,我正在尝试为其实现隐式类型转换。虽然它大部分都有效,但它不适用于界面转换。经过进一步调查,我发现存在编译器错误:适用的“用户定义的接口(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 todeclare a conversion from a sourcetype S to a target type T provided allof the following are true:

  • ...
  • Neither S nor T is object or an interface-type.

User-defined conversions are notallowed to convert from or tointerface-types. In particular, thisrestriction ensures that nouser-defined transformations occurwhen converting to an interface-type,and that a conversion to aninterface-type succeeds only if theobject being converted actuallyimplements the specifiedinterface-type.

关于c# - 使用接口(interface)的隐式运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/143485/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com