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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! 6 1249
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Pete" <an*******@devdex.com> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl... 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 *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
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" <an*******@devdex.com> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl... 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 *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Pete,
I would also review IsAssignableFrom as Nicholas suggested, as it simplifies
GetInterfaces and FindInterfaces in some cases...
Hope this helps
Jay
"Pete" <an*******@devdex.com> wrote in message
news:OM**************@TK2MSFTNGP15.phx.gbl... 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 *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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/<Oe**************@TK2MSFTNGP12.phx.gbl>
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" <an*******@devdex.com> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl... 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 *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
---
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ben |
last post by:
Hi all,
I'm relatively new to C++ and have a quick question concerning a
better way to perform runtime linking. Currently a client wishes to
create a class in a .dll. I export 2 methods from my...
|
by: Charisma |
last post by:
I have a client who is requesting the following:
Description:
-database should be a runtime version of MS Access so that it would not be
necessary for the user to have any Microsoft products in...
|
by: Eric Sassaman [MS] |
last post by:
Online Chat: Visual C++ 2005 Library and Runtime Enhancements
The next release of Visual C++ includes many new and enhanced libraries
which improve security, support managed code and integrate...
|
by: MrB |
last post by:
Hi,
I'm writing an asp.net app in vb.net and many of my ascx classes support an
interface IAutoSave, but some do not.
I want to test in my code if the class behind a given ascx control supports...
|
by: Martin Robins |
last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
| |
by: derekbarrett |
last post by:
Hi,
I found this article in DB2 magazine and learned about the Problem
Determination Mastery Exam. I am very interested in taking the exam,
however, following the links in the article leads to...
|
by: manish |
last post by:
we have avariablr
int bps; // bps determined during run time
-
-
-
how can we make conditional declaration of a variable y
we want appropriate declaration depending upon bps.
|
by: SMcK |
last post by:
I have a PDA-based (Syware Visual CE) database which I need to sync to
an Access database. The Access database contains three tables: 1 is
the data itself, 2 is a linked table that prefills...
|
by: =?Utf-8?B?UHN5cGhlcjg=?= |
last post by:
I really, really want to store some settings somewhere that my control
utilizes to create new object(s)(i.e., class, struct) at runtime without it
knowing about the type it could create at compile...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |