C# - 投射问题

时间:2017-10-14 17:24:09

标签: c# inheritance casting

当尝试将定义自定义泛型的类强制转换为子类时,我得到一个错误。我错了什么?

using System.Collections.Generic;

interface IStorageComponent { }

abstract class CollectionManager<T> where T : IStorageComponent { }

class Bar : IStorageComponent { }

class Foo : CollectionManager<Bar> { }

class Storage
{
    static int Main(string[] args)
    {
        List<CollectionManager<IStorageComponent>> manager = new List<CollectionManager<IStorageComponent>>
        {
            new Foo() // <-- cannot convert from 'Foo' to 'CollectionManager<IStorageComponent>'
        };
        return 0;
    }
}

0 个答案:

没有答案