[英]Implicit Reference Conversion from reference type to interface for user defined types
From C# 4.0 Spec section 6.1.6: 从C#4.0规范第6.1.6节开始:
The implicit reference conversions are:
隐式引用转换为:
[...]
[...]
From any reference-type to an interface or delegate type T if it has an implicit identity or reference conversion to an interface or delegate type T0 and T0 is varience-convertible (13.1.3.2) to T.
从任何引用类型到接口或委托类型T(如果它具有对接口或委托类型T0的隐式标识或引用转换),并且T0可变异(13.1.3.2)到T。
Vladimir Reshetnikov tells us that there is an implicit reference conversion from List<string>
to IEnumerable<object>
. Vladimir Reshetnikov告诉我们,存在从
List<string>
到IEnumerable<object>
的隐式引用转换。 But, how can I apply this to a user defined type (is it even possible)? 但是,如何将其应用于用户定义的类型(甚至可能)?
I tried an implicit operator, custom derived types and a few varitions there-of...but I cannot reproduce the scenerio. 我尝试了隐式运算符,自定义派生类型以及其中的一些变量...但是我无法重现Scenerio。 I have:
我有:
class Program
{
static void Main(string[] args)
{
IEnumerable<object> specialClassConversion = new List<string>();
IEnumerable<A> userdefinedTypeConversion = new List<B>();
A implicitConversion = new B();//varience-convertible
IC<A> explicitConversion = (IC<A>)new D<B>();//OK, varience-convertible
IC<A> implicitConversion2 = new D<B>();//does not compile
}
}
class A { }
class B : A { }
interface IC<T> { }
class D<T>
{
//public static implicit operator IC(D<T> m)//Error: user-defined conversions to or from an interface are not allowed
//{
// return null;
//}
}
If you want a user-defined class or struct to be implicitly convertible to an interface, let your class/struct implement that interface. 如果希望将用户定义的类或结构隐式转换为接口,请让您的类/结构实现该接口。
(Edit) (编辑)
And if you want IC<B>
to be implicitly convertible to IC<A>
, make the IC<T>
interface covariant in T
by specifying the out
keyword, interface IC<out T> { }
. 而且,如果希望
IC<B>
隐式转换为IC<A>
,请通过指定out
关键字interface IC<out T> { }
使IC<T>
接口在T
协变 。 The quote from the spec you gave tells that the "composition" of these two implicit conversion is also an implicit conversion. 您提供的规范中的报价表明,这两个隐式转换的“组成”也是隐式转换。
Source: 资源:
interface IC<out T> { }
class D<T> : IC<T> { }
(End edit) (结束编辑)
Regarding the List<string>
class, it implements IEnumerable<string>
which in turn is convertible (implicitly) to IEnumerable<object>
because IEnumerable<out T>
is covariant ( out
) in T
. 关于
List<string>
类,它实现了IEnumerable<string>
,后者又可以(隐式)转换为IEnumerable<object>
因为IEnumerable<out T>
在T
是协变的 ( out
)。
(One reason why they didn't allow you to make a public static implicit operator
which converts to/from the interface, is that somone could write a derived class which inherited from your class and implemented the interface. That would give a "natural" conversion between their class and the interface, but the public static implicit operator
would also apply, leading to two conversions (one "natural" and one "user-defined") between the types, which would be confusing and ambiguous.) (为什么他们不允许您制作一个
public static implicit operator
来转换为接口或从接口转换,原因之一是somone可以编写派生类,该派生类继承自您的类并实现了该接口。这将产生“自然”它们的类和接口之间的转换,但也将应用public static implicit operator
,从而导致类型之间进行两次转换(一次“自然”转换和一次“用户定义”转换),这会造成混淆和歧义。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.