gpt4 book ai didi

接口(interface)上的 C# 泛型隐式强制转换失败

转载 作者:行者123 更新时间:2023-12-03 13:32:00 25 4
gpt4 key购买 nike

为什么下面不会编译?导致编译器认为它不能从 Container<T> 转换的接口(interface)有什么特别之处?至T , 当 T是接口(interface)吗?我不认为这是一个协变问题,因为我并不沮丧,但也许是。这很像 Why C# compiler doesn't call implicit cast operator?但我不认为完全一样。

Product pIn =null;
Product pOut;
Container<Product> pContainer;

List<Product> pListIn = null;
List<Product> pListOut;
Container<List<Product>> pListContainer;

IList<Product> pIListIn = null;
IList<Product> pIListOut;
Container<IList<Product>> pIListContainer;

pContainer = pIn;
pOut = pContainer; // all good

pListContainer = pListIn;
pListOut = pListContainer; // all good too

pIListContainer = pIListIn; // fails , cant do implicit cast for some reason
pIListOut = pIListContainer; // and here too
class Container<T>
{
private T value;

private Container(T item) { value = item; }

public static implicit operator Container<T>(T item)
{
return new Container<T>(item);
}

public static implicit operator T(Container<T> container)
{
return container.value;
}
}
Cannot implicitly convert type 'Container<IList<Product>>' to 'IList<Product>'. An explicit conversion exists (are you missing a cast?)
Cannot implicitly convert type 'IList<Product>' to 'Container<IList<Product>>'. An explicit conversion exists (are you missing a cast?)

最佳答案

界面上根本不允许用户定义的转换。这可能是模棱两可的,因为您尝试转换的类型可能会实现接口(interface)本身——此时强制转换意味着什么?像普通转换一样的引用转换,还是调用用户定义的转换?

从 C# 4 规范的第 10.3.3 节:

For a given source type S and target type T, if S or T are nullable types, let S0 and T0 refer to their underlying types, otherwise S0 and T0 are equal to S and T respectively. A class or struct is permitted to declare a conversion from a source type S to a target type T only if all of the following are true:

  • S0 and T0 are different types.
  • Either S0 or T0 is the class or struct type in which the operator declaration takes place.
  • Neither S0 nor T0 is an interface-type.
  • Excluding user-defined conversions, a conversion does not exist from S to T or from T to S.


然后:

However, it is possible to declare operators on generic types that, for particular type arguments, specify conversions that already exist as pre-defined conversions
...
In cases where a pre-defined conversion exists between two types, any user-defined conversions between those types are ignored. Specifically:

  • If a pre-defined implicit conversion (§6.1) exists from type S to type T, all user-defined conversions (implicit or explicit) from S to T are ignored.
  • If a pre-defined explicit conversion (§6.2) exists from type S to type T, any user-defined explicit conversions from S to T are ignored. Furthermore:
    • If T is an interface type, user-defined implicit conversions from S to T are ignored.
    • Otherwise, user-defined implicit conversions from S to T are still considered.


注意这里的第一个嵌套项目符号。

(顺便说一下,我完全建议掌握规范。它可以在 various versions and formats 在线获得,但 hardcopy annotated edition 也是团队和其他人的小金矿。我应该在这里承认一些偏见,因为我我是注释者之一 - 但忽略我的东西,所有其他注释都非常值得一读!)

关于接口(interface)上的 C# 泛型隐式强制转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4280592/

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