在一个类中,我处理一个IInterface
的列表。合并两个实现相同接口的类的列表
我想在一个独立的方式来处理的两种可能的实现,因此:
public List<IInterface> Process(List<IInterface> InterfaceList)
{
List<FirstImplementation> FirstList = FirstProcess(InterfaceList.OfType<FirstImplementation>.ToList());
List<SecondImplementation> SecondList = SecondProcess(InterfaceList.OfType<SecondImplementation>.ToList());
return new List<IInterface> {
FirstList,
SecondList
};
}
我想返回List<IInterface>
,相同的输入,无论它原来是比预期的更困难
return new List<IInterface> {
FirstList,
SecondList
};
编译但抛出在运行时一个InvalidCastException,
return new List<IInterface>.AddRange(FirstList).AddRange(SecondList);
甚至没有编译...
什么是正确的方法来做到这一点?
您的最后一个示例几乎是正确的。它缺少括号,'AddRange'返回类型为'void',所以你不能链接在一起:'var ret = new List(); ret.AddRange(FirstList); ret.AddRange(SecondList); return ret;' –