C&C++
发布时间:2022-04-03 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C#split按实现划分的接口列表,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要拆分List< IInterface>
获取IInterface的具体实现列表.
我怎样才能以最佳方式做到这一点?
@H_
403_3@publi
c 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:
@H_
403_3@var grouped = lst.GroupBy(i => i.GetType()).
SELEct(g => g.ToList()).ToList()
如果你想要一个字典,你可以这样做:
@H_
403_3@var grouped = lst.GroupBy(i => i.GetType()).To
Dictionary(g => g.Key,g => g.ToList()
);
var dogList = grouped[typeof(Dog)];
或者蒂姆在评论中提出:
@H_
403_3@var grouped = lst.ToLookup(i => i.GetType()
);
大佬总结
以上是大佬教程为你收集整理的C#split按实现划分的接口列表全部内容,希望文章能够帮你解决C#split按实现划分的接口列表所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。