Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:

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

Public class Car : IVehicle
{
  //Methods implemented
}


Another example of an IVehicle would be

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


In this class I have a list of IVehicles that I would like to find Car from.
C#
Public class Road
{
  List<IVehicle> vehicles = new List<IVehicle>();

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

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


Any help would be greatly appreciated. What is off about my syntax? I've managed to pull Car out, if I make sure to hit the precise index, and the methods run correctly, but I'd like to not have to add in a classes to the List while insuring a precise order is followed. For instance, at one point I want to pull Car out but not Truck. Thanks in advance.
Posted
Updated 30-Nov-13 14:59pm
v2
Comments
BillWoodruff 30-Nov-13 20:54pm    
Please post code that you know can be compiled: there are several reasons your code will not compile now.

Since every instance of Class 'Car inherits from the interface 'IVehicle, then your question doesn't quite make sense to me: your question implies there could be a 'Car which does not inherit from 'IVehicle.

Please clarify your question.
Zerophase 30-Nov-13 21:01pm    
I'm looking for the first instance of Car, (How I'm coding it there is only going to be one.) but I don't want to find instances of Trucks in this example.
BillWoodruff 1-Dec-13 1:38am    
Shifting gears toward the "bigger picture:" have you considered possibly keeping separate generic Lists for each Type of Vehicle ?

Or, you might consider a Dictionary<vehicletype,List<vehicleType>>

If you need to frequently access instances of Classes that implement IVehicle, perhaps not having to bend-over to filter out Classes that are not the flavor you want would pay off in terms of performance ?

1 solution

C#
vehicles.Find(v => v is Car).Speed();
 
Share this answer
 
Comments
Zerophase 30-Nov-13 21:18pm    
Thanks, that did it.
Sergey Alexandrovich Kryukov 30-Nov-13 21:40pm    
It would work, but only for this exact type, Car, not for any descendants. As we don't know exact requiremens from OP (in OOP, "to find Car" is ambiguous, due to possible inheritance), I voted 5 anyway. (Please see my other comment below.)

All correct, my 5.

For OP, I want to note that the whole idea does look like a bad style of code design, violation of OOP.

—SA
BillWoodruff 30-Nov-13 23:29pm    
Sergey, I think that determining if a given class is a descendant of another is the more interesting (potential) aspect of this question. I have never been able to get a solution using Linq to do that, although I am sure there is a way.

So, I might use something like this, if I know that the inheritance tree is only one-level deep:

foreach (IVehicle theVehicle in vehicles)
{
if (theVehicle.GetType().BaseType.Name == "Car")
{
theVehicle.Speed();
break;
}
}
Sergey Alexandrovich Kryukov 30-Nov-13 23:59pm    
Comparing a type name is a really bad solution, much worse then "is Car", especially if you ignore namespaces (some other type can have identical simple name, and this is even a good style to have, in certain situations, identical simple names, I often use it).

But my statement was mistaken: I just forgot that "is" really means the same type or descendant. You can easily test it.

When you deal with Reflection and System.Type, there is another approach:
http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom%28v=vs.110%29.aspx.

—SA
Sergey Alexandrovich Kryukov 1-Dec-13 0:01am    
And if you want to compare exact type, you need to do:

it (myInstance.GetType() == typeof(Car)) {
// then myInstance is of the exact type of Car, not descendant
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900