1

这段代码:

public interface IInter { }
public class Concrete : IInter { /*... body ...*/ }
var t = (List<IInter>)new List<Concrete>();

产生此错误:

Cannot convert type 'System.Collections.Generic.List<Concrete>' to 'System.Collections.Generic.List<IInter>'

为什么 ?我该如何克服它?我的目标是:

var t = new List<List<IInter>>()
{
    new List<ConcreteA>(){/* ... data ... */},
    new List<ConcreteB>(){/* ... data ... */},
    // ...
    new List<ConcreteX>(){/* ... data ... */},
};

编辑:

感谢你的帮助。啊,我有点抽象的东西,使它更容易阅读......但我真正的问题是:

public class SingletonFactory<T> where T : IToken
{
    private SingletonFactory() { }
    private static SingletonFactory<T> _instance = new SingletonFactory<T>();
    public static SingletonFactory<T> Instance { get { return _instance; } }

    public T Produce(int position) { return (T)Activator.CreateInstance(typeof(T), position); }
    public T Produce(int position, string token) { return (T)Activator.CreateInstance(typeof(T), position, token); }
}

进而:

var keywords = new Dictionary<string, SingletonFactory<IToken>>()
{
    { "abc", SingletonFactory<Abc>.Instance },
    { "xyz", SingletonFactory<Xyz>.Instance },
    { "123", SingletonFactory<Num>.Instance }
};

所以我想它要复杂得多......

我正在使用 c# 4.0

4

6 回答 6

2
var res = new List<Concrete>().Cast<IInter>().ToList();
于 2013-01-17T15:22:53.293 回答
2

AList<Concrete> 不是List<IInter>虽然 Concreteimplements IInter。_

您可以使用:

List<IInter> t = ConcreteList.Cast<IInter>().ToList();

协变和逆变常见问题解答

于 2013-01-17T15:23:19.243 回答
2

这称为协变,C# 支持泛型接口而不是泛型类的协变(和逆变)。这是以下作品,但您的示例没有:

IEnumerable<IInter> e = new List<Concrete>();
ICollection<IInter> c = new List<Concrete>();

数组也支持协方差:

IInter[] a = new Concrete[3];
于 2013-01-17T15:26:02.190 回答
2

因为您不能将 a 分配List<ConcreteA>给 a List<IInter>,否则您可以这样做:

concreteAList = newList<ConcreteA>();
List<Inter> interList = concreteAList as List<Inter>;  // seems harmless
interList.Add(new ConcreteB());                        // not allowable

可以这样做:

var t = new List<List<IInter>>()
{
    new List<IInter>(){/* ... fill with ConcreteAs ... */},
    new List<IInter>(){/* ... fill with ConcreteBs ... */},
    // ...
    new List<IInter>(){/* ... fill with ConcreteXs ... */},
};

但我不知道这是否能实现你想要的。

于 2013-01-17T15:27:57.837 回答
1

一种方法是:

IEnumerable<IInter> t = new List<Concrete>();

另一个是:

List<IInter> t = new List<Concrete>().ConvertAll(x => (IInter) x);

编辑,添加更好的样本以表明它有效(测试通过):

[Test]
public void CovarianceTest()
{
    var concrete = new Concrete();
    IEnumerable<IInter> t = new List<Concrete> { concrete };
    Assert.IsTrue(new[] { concrete }.SequenceEqual(t));

    List<IInter> t2 = new List<Concrete> { concrete }.ConvertAll(x => (IInter)x);
    Assert.IsTrue(new[] { concrete }.SequenceEqual(t2));
}
于 2013-01-17T15:23:29.617 回答
1

为您需要从SingletonFactory<T>. 这将确保您的类型 T 是输出类型安全的。

public interface IToken { }
public class Abc : IToken { }
public class Xyz : IToken { }
public class Num : IToken { }

public interface ISingletonFactory<out T> where T : IToken
{
    T Produce(int position);
    T Produce(int position, string token);
}

public class SingletonFactory<T> : ISingletonFactory<T> where T : IToken
{
    private SingletonFactory() { }
    private static SingletonFactory<T> _instance = new SingletonFactory<T>();
    public static SingletonFactory<T> Instance { get { return _instance; } }

    public T Produce(int position) { return (T)Activator.CreateInstance(typeof(T), position); }
    public T Produce(int position, string token) { return (T)Activator.CreateInstance(typeof(T), position, token); }
}

var keywords = new Dictionary<string, ISingletonFactory<IToken>>()
{
    { "abc", SingletonFactory<Abc>.Instance },
    { "xyz", SingletonFactory<Xyz>.Instance },
    { "123", SingletonFactory<Num>.Instance }
};
于 2013-01-25T05:06:52.773 回答