C# 铸造泛型和泛型类型

C# 铸造泛型和泛型类型,c#,generics,casting,covariance,C#,Generics,Casting,Covariance,考虑一下,我有以下3个类/接口: class MyClass<T> { } interface IMyInterface { } class Derived : IMyInterface { } 类MyClass{} 接口IMyInterface{} 派生的类:IMyInterface{} 我希望能够将MyClass转换为MyClass或反之亦然: MyClass<Derived> a = new MyClass<Derived>(); MyClass

考虑一下,我有以下3个类/接口:

class MyClass<T> { }

interface IMyInterface { }

class Derived : IMyInterface { }
类MyClass{}
接口IMyInterface{}
派生的类:IMyInterface{}
我希望能够将
MyClass
转换为
MyClass
或反之亦然:

MyClass<Derived> a = new MyClass<Derived>();
MyClass<IMyInterface> b = (MyClass<IMyInterface>)a;
MyClass a=新的MyClass();
MyClass b=(MyClass)a;
但如果我尝试以下操作,就会出现编译器错误:

Cannot convert type 'MyClass<Derived>' to 'MyClass<IMyInterface>'   
无法将类型“MyClass”转换为“MyClass”
我确信我不能这样做有一个很好的理由,但我想不出一个

至于我为什么要这样做——我想象的场景是,理想情况下,您希望使用
MyClass
的实例,以避免大量讨厌的强制转换,但是您需要将实例传递给接受
MyClass
的接口

所以我的问题有两个:

  • 为什么我不能在这两种类型之间转换
  • 是否有任何方法可以保持使用
    MyClass
    实例的灵活性,同时仍然能够将其转换为
    MyClass

这不起作用,因为C#只支持接口和委托类型参数的协方差。如果您的类型参数只存在于输出位置(即,您只从类返回它的实例,不接受它作为参数),您可以创建如下接口:

interface IClass<out T> { }
class MyClass<T> : IClass<T> { }
接口类{}
类MyClass:IClass{}
这将允许您执行以下操作:

IClass<Derived> a = new MyClass<Derived>();
IClass<IMyInterface> b = a;
IClass a=新的MyClass();
I类b=a;

老实说,这是你将要得到的最接近的结果,这需要C#4编译器工作。

一般来说,你不能这样做的原因是因为大多数类不是简单的空示例。他们有以下方法:

class MyClass<T> 
{
    static T _storage;

    public void DoSomethingWith(T obj)
    {
        _storage = obj;
    }
}

interface IMyInterface { }

class Derived : IMyInterface { }

MyClass<Derived> a = new MyClass<Derived>();
如果允许的话,
b
现在似乎有一个方法
DoSomethingWith
,它接受任何实现
IMyInterface
,然后在内部尝试将其存储在类型为
派生的
的静态变量中,因为它仍然是
a
引用的同一个对象

现在你有了一个
Derived
类型的变量来存储。。。谁知道呢

MyClass<IMyInterface> b = (MyClass<IMyInterface>)a;