我怎样才能以最佳方式做到这一点?
public interface IPet { } public class Dog :IPet { } public class Cat : IPet { } public class Parrot : IPet { } public voID Act() { var lst = new List<IPet>() {new Dog(),new Cat(),new Parrot()}; // I need to get three Lists that hold each implementation // of IPet: List<Dog>,List<Cat>,List<Parrot> }解决方法 您可以按类型执行GroupBy:
var grouped = lst.GroupBy(i => i.GetType()).Select(g => g.ToList()).ToList()
如果你想要一个字典,你可以这样做:
var grouped = lst.GroupBy(i => i.GetType()).ToDictionary(g => g.Key,g => g.ToList());var dogList = grouped[typeof(Dog)];
或者蒂姆在评论中提出:
var grouped = lst.Tolookup(i => i.GetType());总结
以上是内存溢出为你收集整理的C#split按实现划分的接口列表全部内容,希望文章能够帮你解决C#split按实现划分的接口列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)