如何在列表中搜索接口的特定实现。 [英] How to search a list for specific implementation of an interface.

查看:107
本文介绍了如何在列表中搜索接口的特定实现。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索列表中的接口实现,但无法找出用于查找特定实现的语法。



我的基本看起来像这个:



I'm trying to search a list for implementations of an interface, but cannot figure the syntax out for finding specific implementations.

What I have basically looks like this:

public interface IVehicle
{
  void Speed()
  void Weight()
}

Public class Car : IVehicle
{
  //Methods implemented
}





另一个IVehicle的例子是





Another example of an IVehicle would be

public class Truck : IVehicle
{
  //Some Methods to implement
}





是我有一个IVehicles列表,我想找到Car来自。



In this class I have a list of IVehicles that I would like to find Car from.

Public class Road
{
  List<IVehicle> vehicles = new List<IVehicle>();

  void SomeMethod()
  {
    vehicles.Add(new Car);

    //How would I search for a Car.
  }
}





任何帮助将不胜感激。关于我的语法是什么?我已经设法将Car拉出来,如果我确保打到精确的索引,并且方法正确运行,但我不想在确保按照精确的顺序确保订单时添加类。例如,有一次我想把车拉出来而不是卡车。在此先感谢。

推荐答案

vehicles.Find(v => v is Car).Speed();


这篇关于如何在列表中搜索接口的特定实现。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆