How to determine if a type implements an interface with C# reflection

c# check if type implements generic interface
c check if type implements abstract class
c check if type implements generic interface
reflection get implemented interfaces c
reflection check if class implements interface java
c# check if object is of type
vb net check if type implements interface
unity check if object implements interface

Does reflection in C# offer a way to determine if some given System.Type type models some interface?

public interface IMyInterface {}

public class MyType : IMyInterface {}

// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);

You have a few choices:

  1. typeof(IMyInterface).IsAssignableFrom(typeof(MyType))

  2. typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))

For a generic interface, it’s a bit different.

typeof(MyType).GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMyInterface<>))

C# test if object or type implements interface, To test if an object implements a certain interface: if(myObject is IMyInterface) { // object myObject implements IMyInterface. } if(typeof(IMyInterface).IsAssignableFrom(typeof(MyType))) { // type MyType implements IMyInterface. } var myCastedObject = myObject as IMyInterface; if(myCastedObject != null) { As you can see, I'm simply checking whether a type name contains my filterCriteria. Now I am ready to call Type.FindInterfaces to perform my check. As FindInterfaces returns an array of types (Type []), if the length of the returned array is greater than zero we know the passed in interface has been implemented or inherited.

Use Type.IsAssignableFrom:

typeof(IMyInterface).IsAssignableFrom(typeof(MyType));

Does a Type Implement an Interface?, Type and he wants to see if that type is IWhateverable. Plus, IsAssignableFrom doesn't go through reflection like GetInterface, so it you're missing my FAVORITE method for checking to see if a type implements a specific interface. I'm no C# guru, but doesn't the "as" C# keyword do this even better? Answers: Modifying Jeff’s answer for optimal performance (thanks to performance test by Pierre Arnaud): var type = typeof (MyType); var implementsInterface = typeof (IMyInterface).IsAssignableFrom (type) && type.IsClass; To find all types that implement an interface in a given Assembly: var implementations = typeof (TypeInTargetAssembly).Assembly.GetTypes () .Where (t => typeof (IMyInterface).IsAssignableFrom (t) && t.IsClass);

typeof(IMyInterface).IsAssignableFrom(someclass.GetType());

or

typeof(IMyInterface).IsAssignableFrom(typeof(MyType));

How to check if a type implements a specific interface using C# , post I am going to show a really useful reflection method which allows us to check whether a type implements or inherits a specific interface. Using Anton Tykhyy's proposal, here is a small Extension Method to check if some type implements a generic interface with one given generic type parameters: public static class ExtensionMethods { /// <summary> /// Checks if a type has a generic interface.

public static bool ImplementsInterface(this Type type, Type ifaceType) 
{
    Type[] intf = type.GetInterfaces();
    for(int i = 0; i < intf.Length; i++) 
    {
        if(intf[ i ] == ifaceType) 
        {
            return true;
        }
    }
    return false;
}

I think this is the correct release, for three reasons:

  1. It uses GetInterfaces and not IsAssignableFrom, it's faster since IsAssignableFrom eventually after several checks does call GetInterfaces.
  2. It iterates over the local array, so there will be no bounds checks.
  3. It uses the == operator which is defined for Type, so probably is safer than the Equals method (that the Contains call, will eventually use).

Type.IsSubclassOf(Type) Method (System), Determines whether the current Type derives from the specified Type. It calls the IsSubclassOf method to show that DerivedC1 is a subclass of Class1 . C# Copy derives from another interface, or whether a class implements an interface. Totally aggree, but in my case I just want to do a basic check in a Attribute [CustomAttribute(typeof(ClassName))] and I require ClassName to implement the interface.Later in code, I know both generic attributes and can use reflection to create a new instance of ClassName – Jürgen Steinblock Aug 15 '13 at 6:39

I just did:

public static bool Implements<I>(this Type source) where I : class
{
  return typeof(I).IsAssignableFrom(source);
}

I wish I could have said where I : interface, but interface is not a generic parameter constraint option. class is as close as it gets.

Usage:

if(MyType.Implements<IInitializable>())
  MyCollection.Initialize();

I just said Implements because that's more intuitive. I always get IsAssignableFrom flip-flopped.

Get All C# Classes Implementing an Interface, For Instance, if you notice that a class implements the IDomainEntity ask the application itself to determine the list of entities that require flushing. C# and Reflection in the .net framework are powerful tools to have in your  Note that there can be numerous interfaces derived from IFoo and you do not necessarily know about their existence. The obvious method is to find all interfaces derived from IFoo through reflection and then just check the typeof (Bar).GetInterfaces () is any of those are present in there.

How to determine if a type implements an interface with C# reflection, reflection get implemented interfaces c# vb net check if type implements interface reflection check if class implements interface java c# check if object is of type How to determine if a type implements an interface with C# reflection (8) Does reflection in C# offer a way to determine if some given System.Type type models some interface? public interface IMyInterface {} public class MyType : IMyInterface {} // should yield 'true' typeof (MyType).

Determine if a Type implements an interface, Unlike Java, where the “Class“ can be constructed and checked for inheritance, the Type in Apex is fairly limited. One of the few operations it permits is  C# test if object or type implements interface September 29, 2013 C# Snippet If you have a type or an instance you can easily check if they support a specific interface.

Identifying all Types that Implement an Interface, Loop through the types and use reflection methods to determine which of the types implement the target interface. To demonstrate, create a new  "Returns true if the c parameter and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c supports." I suggested this, which is his original idea with the string literal coming from elsewhere: (C#)

Comments
  • Remember that typeof(IMyInterface).IsAssignableFrom(typeof(IMyInterface)) is also true, which may have an unexpected result on your code.
  • It sure was easy to not pay attention and get the arguments for IsAssignableFrom backwards. I will go with GetInterfaces now :p
  • The IsAssignableFrom(t1) variant is about 3x faster than the GetInterfaces().Contains(t2) counterpart in my code.
  • @PierreArnaud: IsAssignableFrom does eventually calls GetInterfaces, so probably your test checked the GetInterfaces first and IsAssignable after. That is because GetInterfaces caches it's results so the first invocation costs more
  • A small change to @Kosta's answer. With C# 6 we can do typeof(MyType).GetInterface(nameof(IMyInterface)) != null for better type safety and refactoring.
  • If you already have an instance of the class a much better approach is simply someclass is IMyInterface as that doesn't involve the cost of reflection at all. So, while not wrong, its not an ideal way to do it.
  • @James - Agree. Even Resharper gives the same suggestion.
  • @JamesJ.ReganIV you should post that as an answer, I almost missed your comment
  • @reggaeguitar, thanks, but the comment doesn't answer the original question. The question asks for the Reflection solution, I am just saying in this answer's first case where you do have an instance of the object reflection isn't the ideal solution.
  • @JamesJ.ReganIV Actually, is checks in both directions of the inheritance hierarchy whereas IsAssignableFrom only checks upwards. Also, if you have an instance of an object, you should call IsInstanceOfType (which also only looks upwards).
  • +1 for content, I hate the spaces around the parens and the Egyptian braces though. Also the whole method can be written as: return type.GetInterfaces().Any(t => t == ifaceType);