Tutorial :Detect Class from Interface Type


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?      }  }  


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


if (theTypes[i] == typeof(Bar))  {      // t is Bar  }   else if (theTypes[i] == typeof(Baz))  {      // t is Baz  }  


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        }    }  


I think this will help with your problem:

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


Am I missing something?

theTypes[i] is the type.


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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »