这可能有一个问题,但我没有能够拿出搜索条件来找到答案..IEnumerable中不能使用泛型类型的约束?
我可能在这里丢失了一些明显的东西,但为什么我不允许请执行以下操作,这会给出错误:
"Argument 1: cannot convert from
System.Collections.Generic.IEnumerable<TType>
toSystem.Collections.Generic.IEnumerable< Test.A>
"
同时拨打DoSomething的电话?
public interface A
{
void Foo();
}
public class B : A
{
public void Foo()
{
}
}
class Test<TType> where TType : A
{
public Test(IEnumerable<TType> testTypes)
{
DoSomething(testTypes);
}
void DoSomething(IEnumerable<A> someAs)
{
}
}
虽然它是的,当然,OK做到这一点:
class Test
{
public Test(IEnumerable<B> testTypes)
{
DoSomething(testTypes);
}
void DoSomething(IEnumerable<A> someAs)
{
}
}
差异。如果'TTypes'是'struct',那么'IEnumerable'不能被转换成'IEnumerable '。如果你为'TType'增加额外的'class'约束,那么cast将被允许。 –
PetSerAl
谢谢 - 这是有道理的。对我来说代码不是一个很大的问题,我只是很好奇,当我试图做到这一点,并得到了错误。编辑:看到你编辑你的回应,建议在where子句中使用class - 谢谢! – Matt