377
votes

What is the simplest way of testing if an object implements a given interface in C#? (Answer to this question in Java)

12

12 Answers

611
votes
if (object is IBlah)

or

IBlah myTest = originalObject as IBlah

if (myTest != null)
233
votes

Using the is or as operators is the correct way if you know the interface type at compile time and have an instance of the type you are testing. Something that no one else seems to have mentioned is Type.IsAssignableFrom:

if( typeof(IMyInterface).IsAssignableFrom(someOtherType) )
{
}

I think this is much neater than looking through the array returned by GetInterfaces and has the advantage of working for classes as well.

26
votes

If you want to use the typecasted object after the check:
Since C# 7.0:

if (obj is IMyInterface myObj)

This is the same as

IMyInterface myObj = obj as IMyInterface;
if (myObj != null)

See .NET Docs: Pattern matching overview

24
votes

For the instance:

if (obj is IMyInterface) {}

For the class:

Check if typeof(MyClass).GetInterfaces() contains the interface.

16
votes

A variation on @AndrewKennan's answer I ended up using recently for types obtained at runtime:

if (serviceType.IsInstanceOfType(service))
{
    // 'service' does implement the 'serviceType' type
}
8
votes

This Post is a good answer.

public interface IMyInterface {}

public class MyType : IMyInterface {}

This is a simple sample:

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

or

typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))
3
votes

In addition to testing using the "is" operator, you can decorate your methods to make sure that variables passed to it implement a particular interface, like so:

public static void BubbleSort<T>(ref IList<T> unsorted_list) where T : IComparable
{
     //Some bubbly sorting
}

I'm not sure which version of .Net this was implemented in so it may not work in your version.

3
votes

What worked for me is:

Assert.IsNotNull(typeof (YourClass).GetInterfaces().SingleOrDefault(i => i == typeof (ISomeInterface)));

2
votes

Recently I tried using Andrew Kennan's answer and it didn't work for me for some reason. I used this instead and it worked (note: writing the namespace might be required).

if (typeof(someObject).GetInterface("MyNamespace.IMyInterface") != null)
1
votes

I used

Assert.IsTrue(myObject is ImyInterface);

for a test in my unit test which tests that myObject is an object which has implemented my interface ImyInterface.

0
votes

I had a situation where I was passing a variable to a method and wasn't sure if it was going to be an interface or an object.

The goals were:

  1. If item is an interface, instantiate an object based on that interface with the interface being a parameter in the constructor call.
  2. If the item is an object, return a null since the constuctor for my calls are expecting an interface and I didn't want the code to tank.

I achieved this with the following:

    if(!typeof(T).IsClass)
    {
       // If your constructor needs arguments...
       object[] args = new object[] { my_constructor_param };
       return (T)Activator.CreateInstance(typeof(T), args, null);
    }
    else
       return default(T);
-12
votes

This should work :

MyInstace.GetType().GetInterfaces();

But nice too :

if (obj is IMyInterface)

Or even (not very elegant) :

if (obj.GetType() == typeof(IMyInterface))