views:

152

answers:

6

I have an interface, and some classes that inherit from it.

public interface IFoo {}
public class Bar : IFoo {}
public class Baz : IFoo {}

If I get the types which implement IFoo, how can I decide if the type will represent a Bar or a Baz (without actually creating the object)?

// Get all types in assembly.
Type[]    theTypes = asm.GetTypes();

// See if a type implement IFoo.
for (int i = 0; i < theTypes.Length; i++)
{
    Type    t = theTypes[i].GetInterface("IFoo");
    if (t != null)
    {
        // TODO: is t a Bar or a Baz?
    }
}
A: 

Am I missing something?

theTypes[i] is the type.

leppie
+4  A: 
if (theTypes[i] == typeof(Bar))
{
    // t is Bar
} 
else if (theTypes[i] == typeof(Baz))
{
    // t is Baz
}
Darin Dimitrov
+3  A: 

t is neither Bar nor Baz - it is IFoo. theTypes[i] is Bar or Baz.

Marc Gravell
Okay, so I tested theTypes[i] using IsSubclassOf...is that the correct comparison method? Plain `==` does not seem to work.
Nick
+2  A: 

When you do GetInerface, you're getting the interface only. What you need to do is only get the types that implement that interface like so.

var theTypes = asm.GetTypes().Where(
                                    x => x.GetInterface("IFoo") != null
                                    );

now you can loop through them and do this. or use a switch.

foreach ( var item in theTypes )
  {
     if ( item == typeof(Bar) ) 
      {
         //its Bar
      }
     else if ( item == typeof(Baz) )
      {
        ///its Baz
      }
  }
Stan R.
A: 

A strongly-typed solution to "Does Type X implement interface I" that supports analysis/refactoring is:

Type x = ...;
bool implementsInterface = Array.IndexOf(x.GetInterfaces(), typeof(I)) >= 0;

That said, I really have no idea what you are attempting to accomplish.

280Z28
It's more like, "take all the things which implement interface I and put them in lists based on the type". The original implementation actually constructed every single object, which is obviously wasteful of memory and slow. I'm just storing the types not and building the objects on demand.
Nick
A quicker, way to do this is bool implementsInterface = typeof(IFoo).IsAssignableFrom(x);
Andrew Kennan
+1  A: 

I think this will help with your problem:

IFoo obj = ...;
Type someType = obj.GetType();
if (typeof(Bar).IsAssignableFrom(someType))
    ...
if (typeof(Baz).IsAssignableFrom(someType))
    ...
Veton