Runtime Determination of Interface Support

  • Thread starter Thread starter Pete
  • Start date Start date
P

Pete

Hi,

Is it possible for an application to determine, at runtime, whether an
arbitrary class supports a known interface?

I can think of a couple of cludgy ways of doing this (the calling app
will know the details of the interface, so worst-case could just blindly
try invoking a method) but I'm scanning through the reflection
documentation to find a clean way of doing this, and haven't found
anything thus far.

Thanks,
Pete
 
Pete,

If you have an instance, then you can easily do this:

// instance is the instance, IMyInterface is the interface.
bool derives = ((instance as IMyInterface) != null);

However, if you have the Type, then you can call the GetInterfaces
method of the Type to get the interfaces that the type implements. You can
then cycle through the array and see if your interface is in there (just do
a comparison on the type of the interface).

If you have one interface, then the easiest way to do this would be to
call the IsAssignableFrom method on the Type, passing in the interface type.
If it returns true, then you know that it can be assigned to it.

Finally, the FindInterface method can also be used to filter the
interfaces, but that requires a little work.

Hope this helps.
 
Pete,
In addition to the other comments (the as operator).

You can also use the is operator.

object anObject;
if (anObject is IDisposable)
{
IDisposable disposable = (IDisposable)anObject;
// do something with disposable
}

Hope this helps
Jay
 
Excellent, thanks.

Sorry I should have said that I didn't have an instance of the class,
that would have made the question a little clearer.

I'll start fishing around GetInterfaces and FindInterfaces.

Thanks once again.
Pete
 
Pete,
I would also review IsAssignableFrom as Nicholas suggested, as it simplifies
GetInterfaces and FindInterfaces in some cases...

Hope this helps
Jay
 
Nicholas Paldino said:
If you have an instance, then you can easily do this:

// instance is the instance, IMyInterface is the interface.
bool derives = ((instance as IMyInterface) != null);

Just a note that a slightly prettier way of doing this would be:

bool derives = instance is IMyInterface;

The "as" operator is useful when you're going to use the cast value
afterwards, but if you're *only* going to test whether or not it's
null, "is" is more readable.
However, if you have the Type, then you can call the GetInterfaces
method of the Type to get the interfaces that the type implements. You can
then cycle through the array and see if your interface is in there (just do
a comparison on the type of the interface).

Or use Array.IndexOf, indeed.

It's nice that GetInterfaces includes "base" interfaces too. For
instance:

using System;

interface IBase
{
void Bar();
}

interface IDerived : IBase
{
}

class Test : IDerived
{
public void Bar()
{
}

static void Main()
{
foreach (Type t in typeof(Test).GetInterfaces())
{
Console.WriteLine(t.Name);
}
}
}

outputs IDerived and IBase rather than just IDerived.
If you have one interface, then the easiest way to do this would be to
call the IsAssignableFrom method on the Type, passing in the interface type.
If it returns true, then you know that it can be assigned to it.

Ah, IsAssignableFrom. That's one of those methods I always have to
think about for about 30 seconds before working out which way round
it's meant to go...
Finally, the FindInterface method can also be used to filter the
interfaces, but that requires a little work.

Ooh, hadn't seen that before. Interesting.
 
One thing to remember with this approach is that the type check will be performed twice compared to the as operatorss once. Its much faster to compare a reference to null than to perform a type check.

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Oeoyv#[email protected]>

Pete,
In addition to the other comments (the as operator).

You can also use the is operator.

object anObject;
if (anObject is IDisposable)
{
IDisposable disposable = (IDisposable)anObject;
// do something with disposable
}

Hope this helps
Jay

Pete said:
Hi,

Is it possible for an application to determine, at runtime, whether an
arbitrary class supports a known interface?

I can think of a couple of cludgy ways of doing this (the calling app
will know the details of the interface, so worst-case could just blindly
try invoking a method) but I'm scanning through the reflection
documentation to find a clean way of doing this, and haven't found
anything thus far.

Thanks,
Pete



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Back
Top