Search this site:

2008-01-28

C#: Determining whether a class implements certain interface


Sometimes it is useful to check whether a class (or its instance) implements certain interface. I found a good thread today discussing the issue:

How to determine base interface??? - .NET C#


If you are too busy (well, or too lazy) to dig the answer out of the long page, here is the solution:
typeof(IFoo).IsAssignableFrom(bar.GetType());
typeof(IFoo).IsAssignableFrom(typeof(BarClass));

The Type.IsAssignableFrom() method check whether a type can be assigned from another. It takes a System.Type object as argument, so if you are given a class or an instance of a class, you can use the typeof operator or the Object.GetType() method to get the corresponding System.Type object respectively. The MSDN has a clear definition of the returned value of the IsAssignableFrom() method:
Return Value
Type: System.Boolean

true if c 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 implements, or if c is a generic type parameter and the current Type represents one of the constraints of c. false if none of these conditions are true, or if c is a null reference.
As you may have noticed, the IsAssignableForm() method can be applied to all System.Type object, so you can definitely use it to determine whether a class is derived (directly or indirectly) from another. But the case could be more complicated. When you have an interface and a class, the only possible relation is the class (or any class in its class hierarchy) implements the interface. But when you have two classes and try to find out their relation, one could be either the same as, the base or a derivative of another. For the first two case the IsAssignableForm() method returns a true value. Instead of explicitly checking whether two classes being the same, you may use the Type.isSubClassOf() method instead:
typeof(FooClass).isSubClassOf(typeof(BarClass));

Let me use the following examples as a summary:
interface I { /* ... */ }

class A : I { /* ... */ }

class B : A { /* ... */ }

/* something */

Console.WriteLine(
typeof(A).isAssignableFrom(typeof(I))); // false
Console.WriteLine(typeof(A).isSubClassOf(typeof(I))); // false

Console.WriteLine(
typeof(I).isAssignableFrom(typeof(A))); // true
Console.WriteLine(typeof(I).isAssignableFrom(typeof(B))); // true
Console.WriteLine(typeof(B).isSubClassOf(typeof(I))); // false

Console.WriteLine(
typeof(A).isAssignableFrom(typeof(A))); // true
Console.WriteLine(typeof(A).isSubClassof(typeof(A))); // false

Console.WriteLine(
typeof(A).isAssignableFrom(typeof(B))); // true
Console.WriteLine(typeof(A).isSubClassof(typeof(B))); // false

Console.WriteLine(
typeof(B).isAssignableFrom(typeof(A))); // false
Console.WriteLine(typeof(B).isSubClassof(typeof(A))); // true

/* something else */


15 comments:

  1. Thank you!!
    Just what I needed.
    :-)

    ReplyDelete
  2. You are welcome! Glad to hear my works helped someone, that's the joy for sharing! :-D

    ReplyDelete
  3. I just found your article from a google search. Very concise and to the point. Keep 'em rolling!

    ReplyDelete
  4. Thanks for your kind words! I will try to share more here. But unfortunately I am quite busy recently, wish to have time writing more soon!

    ReplyDelete
  5. Thank you, saves me time, now I can sleep before night :-)

    Cheers,

    Rishi

    ReplyDelete
  6. Cheers, exactly what i needed :)

    ReplyDelete
  7. This will not work faithfully if you are trying to determine if an instance implements a generic type. Here's a link for working with generics: http://www.dimebrain.com/2008/12/how-to-determine-generic-inheritance-and-generic-parameters-at-runtime.html

    ReplyDelete
  8. class A<T>
    {
    }
    class B : A<int>
    {
    }
    class C : A<string>
    {
    }

    I am looking for a function that could return true when presented the typeof B and C, because they are A<>. Couldn't find a way yet.

    Current I solve it by declaring the A
    class A<T> : MyInterface
    {
    }
    and check for MyInterface instead.

    P.S. 哈哈哈...我也是被google帶到這裏,世界真細小。Search term "c# instance type of a generic class"

    ReplyDelete
  9. Oh the search term should be "c# instance type of a generic class issubclassof" :)

    ReplyDelete
  10. Just what i was after!
    Cheers

    ReplyDelete
  11. Exactly what I needed to know.

    Thanks!

    ReplyDelete
  12. It's things like this that prove why the internet is so useful! Many thanks, solved my problem after 2 mins of searching

    ReplyDelete
  13. Nice Article.. Helped me lot..

    ReplyDelete
  14. This works for me. Thanks ....

    ReplyDelete

HTML Tags allowed (e.g. <b>, <i>, <a>)

Disclaimer

ALL CONTENTS AND INFORMATION IN THIS WEB SITE ARE PROVIDED "AS IT" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE ACCURACY AND AVAILABILITY OF THE CONTENTS, INFORMATION AND THE WEB SITE ITSELF ARE NOT GUARANTEED. THE AUTHOR TAKES NO RESPONSIBILITIES ON ANY COSTS OR DAMAGES (DIRECT OR INDIRECT) ARISING OUT OF OR IN CONNECTION WITH THE ACCESS, USAGE OR INABILITY OF USAGE OF THIS WEB SITE.