Tutorial :Detect Class from Interface Type



Question:

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


Solution:1

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


Solution:2

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


Solution:3

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


Solution:4

I think this will help with your problem:

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


Solution:5

Am I missing something?

theTypes[i] is the type.


Solution:6

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
Previous
Next Post »