« Infoworld SOA Executive Forum | Main | Gaming at 16 by 10 on a Widescreen LCD »

Does a Type Implement an Interface?

Posted 2005-10-28 11:31 AM in NDoc.

There was an interesting discussion on a mailing list I'm on recently, where a fellow asked: "Given a Type object representing a given class, how do you determine if that class implements a specific interface?"

To be clear, he's not asking how to do:

(C#)
if (myType is IWhateverable) { ... }

He has an object of type System.Type and he wants to see if that type is IWhateverable. He could do this:

(VB)
If myType.GetInterface("MyClass.IWhateverable") IsNot Nothing Then

But he feels, perhaps rightfully so, that the string literal stuck in there is distasteful.

One fellow said, why not run through the interfances with a helper function:

(VB)
Function IsImplemented(objectType As Type, intefaceType As Type) As Boolean
    For Each thisInterface As Type in objectType.GetInterfaces
        If thisInterface Is interfacetype Then
            Return True
        Next
    Next
End Function

The next said, why not:

(C#)
if (typeof(IWhateverable).IsAssignableFrom(myType)) { ... }

Which isn't bad, but the semantics of IsAssignableFrom are a little more "inclusive" than you might want. From MSDN:

"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#)
if (myType.GetInterface(typeof(IWhateverable).FullName) { ... }

However, it's a shame there isn't a built in:

(C#)
if (myFooInstance.IsImplementationOf(typeof(IWhateverable))) { ... }

Which, arguably, would just do what the IsImplemented definition at the top of this post would do internally! :)

UPDATE: Wesner says I'm using IsAssignableFrom wrong. Yes, I think I reversed the semantics there. Fixed. It's still up in the air if it's more correct or faster as he implies it may be. Check the comments for the ongoing thread.



Friday, October 28, 2005 2:20:57 PM (Pacific Standard Time, UTC-08:00)
Correct me, if I am wrong... but I believe that you want IsAssignableFrom and that you use the function incorrectly. Plus, IsAssignableFrom doesn't go through reflection like GetInterface, so it should be orders faster.

typeof(IWhatever).IsAssignableFrom(type)

Friday, October 28, 2005 2:33:05 PM (Pacific Standard Time, UTC-08:00)
Wes,

Not sure. Yes and No I think. Yes, I think I reverse it, but no, when I Reflector into Type.IsAssignableFrom I see this:

Type[] typeArray2 = c.GetInterfaces();
for (int num2 = 0; num2 < typeArray2.Length; num2++)
{
if (this == typeArray2[num2])
{
return true;
}
}

Which looks like the same thing in the other examples, and uses GetInterfaces.

Friday, October 28, 2005 3:03:55 PM (Pacific Standard Time, UTC-08:00)
Scott,

you're missing my FAVORITE method for checking to see if a type implements a specific interface. The "cast-and-catch" method.
try
{
IWhateverable foo = (IWhateverable) myObject;
}
catch (InvalidCastException ex)
{
//oops
}

(ducking and running)
Friday, October 28, 2005 3:21:16 PM (Pacific Standard Time, UTC-08:00)
> IWhateverable foo = (IWhateverable) myObject;

I'm no C# guru, but doesn't the "as" C# keyword do this even better?
Friday, October 28, 2005 3:31:36 PM (Pacific Standard Time, UTC-08:00)
ScottKoon and Jeff,

Again, we're talking about objects of type "Type." These aren't object instances. We want to know if a System.Type instance implements an interface.

Yes, Jeff, "as" does what Scott mentions, but he's (I hope) trying to be funny. Trying. ;)
Friday, October 28, 2005 5:13:23 PM (Pacific Standard Time, UTC-08:00)
IsAssignableFrom is definitely what you want (although it does take a bit of getting used to since it seems like you have to use it "backwards" - IsAssignableTo either instead of or in addition to, perhaps).

The only gotcha is if you loaded one of the two types involved in the IsAssignableFrom through ReflectionOnlyLoad and the other not, but not many people get bit by that.
Saturday, October 29, 2005 9:34:38 AM (Pacific Standard Time, UTC-08:00)
Jeff, et al.

The "as" keyword doesn't throw the invalid cast exception, it just returns null. (I believe). Plus it's not nearly as funny. But given that Scott H.had to explain my attempt at humor, maybe a direct cast isn't funny either. ;)
Saturday, October 29, 2005 1:11:34 PM (Pacific Standard Time, UTC-08:00)
I believe your last suggestion must be wrong:<br /><br />if (myFooInstance.IsImplementationOf(typeof(IWhateverable))) { ... } <br /><br />If you already have an instance, and a type to check, the following would do just fine, as you said at the beginning:<br /><br />if (myFooInstance is IWhateverable)<br /><br />I think sometihng like the following would be better:<br /><br />if (theTypeToCheck.Implemnets(theInterfaceType))<br /><br />it would be the natural complement to Type.IsAssignableFrom (which I believe would have been better off being IsAssignableTo ;))<br /><br /><br />Daniel Cazzulino [XML MVP]
Monday, October 31, 2005 8:55:59 PM (Pacific Standard Time, UTC-08:00)
Hmmm... One other point, IsAssignableFrom uses GetInterfaces() not GetInterface(string), which is going to be faster simply because it avoids any string comparison.
Comments are closed.

Contact

Sponsors

Hosting By

Hot Topics

Tags

Calendar

<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

November, 2009 (2)
October, 2009 (19)
September, 2009 (11)
August, 2009 (12)
July, 2009 (21)
June, 2009 (26)
May, 2009 (16)
April, 2009 (13)
March, 2009 (17)
February, 2009 (17)
January, 2009 (18)
December, 2008 (32)
November, 2008 (17)
October, 2008 (22)
September, 2008 (16)
August, 2008 (14)
July, 2008 (25)
June, 2008 (19)
May, 2008 (17)
April, 2008 (17)
March, 2008 (26)
February, 2008 (21)
January, 2008 (28)
December, 2007 (19)
November, 2007 (17)
October, 2007 (31)
September, 2007 (39)
August, 2007 (37)
July, 2007 (43)
June, 2007 (37)
May, 2007 (32)
April, 2007 (38)
March, 2007 (29)
February, 2007 (46)
January, 2007 (31)
December, 2006 (27)
November, 2006 (31)
October, 2006 (32)
September, 2006 (39)
August, 2006 (34)
July, 2006 (40)
June, 2006 (18)
May, 2006 (31)
April, 2006 (34)
March, 2006 (30)
February, 2006 (38)
January, 2006 (44)
December, 2005 (19)
November, 2005 (34)
October, 2005 (24)
September, 2005 (37)
August, 2005 (20)
July, 2005 (24)
June, 2005 (33)
May, 2005 (16)
April, 2005 (22)
March, 2005 (34)
February, 2005 (15)
January, 2005 (37)
December, 2004 (28)
November, 2004 (30)
October, 2004 (34)
September, 2004 (22)
August, 2004 (34)
July, 2004 (18)
June, 2004 (64)
May, 2004 (49)
April, 2004 (21)
March, 2004 (29)
February, 2004 (29)
January, 2004 (36)
December, 2003 (25)
November, 2003 (24)
October, 2003 (59)
September, 2003 (42)
August, 2003 (24)
July, 2003 (44)
June, 2003 (29)
May, 2003 (21)
April, 2003 (30)
March, 2003 (27)
February, 2003 (47)
January, 2003 (50)
December, 2002 (31)
November, 2002 (38)
October, 2002 (44)
September, 2002 (15)
May, 2002 (2)
April, 2002 (4)

Google Ads