简体   繁体   English

用户定义类型从引用类型到接口的隐式引用转换

[英]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.

相关问题 没有从“类型”到“接口”的隐式引用转换 - There is no implicit reference conversion from 'Type' to 'Interface' 通用接口具体类型之间没有隐式引用转换错误 - No implicit reference conversion error between generic interface concrete type 即使定义了转换,“没有隐式引用转换” - "no implicit reference conversion" even though conversion is defined 使用继承接口的类型约束会导致隐式引用转换错误 - Using type constraints from inherited interface gives implicit reference conversion error 通用类型:没有从ToolStripStatusLabel到Control的隐式引用转换 - Generic Types: There is no implicit reference conversion from ToolStripStatusLabel to Control 没有从“type1”到“type2”的隐式引用转换 - There is no implicit reference conversion from 'type1' to 'type2' 在泛型中没有从“派生类型”到“基本类型”的隐式引用转换 - There is no implicit reference conversion from “Derive Type” to “Base type” in Generics 从具有类型约束的类继承-“不存在来自…的隐式引用转换” - Inheriting from class with type constraints - “There is no implicit reference conversion from…” c#类型推断 - 错误 - 没有隐式引用转换 - c# Type Inference - Error - There is no implicit reference conversion from 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM