473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to check if CLASS implements an interface?

Ok, I am trying to figure out how to tell if a class (not object) implements
a particular interface. Using the "is" operator does not seem to work for
me and I assume its because I have a class, not an object I am wanting to
check.

Here's my scenario. I am dynamically loading assemblies (dlls) at runtime.
I am getting the list of types from each assembly, and if that type
implements a particular interface, then and only then do I want to
instantiate one. I have seen several articles showing how to do the dynamic
loading, but I haven't seen anyone address this issue. Perhaps in a given
assembly, there are helper types that do not implement the interface and I
wouldn't want to instantiate one of them. How can I verify that a type
(System.Type) implements a particular interface?

Thanks.
Nov 17 '05 #1
5 16906
IsAssignableFrom

"Joe Rattz" <jo******@yahoo.com> wrote in message
news:OK**************@TK2MSFTNGP15.phx.gbl...
Ok, I am trying to figure out how to tell if a class (not object)
implements
a particular interface. Using the "is" operator does not seem to work for
me and I assume its because I have a class, not an object I am wanting to
check.

Here's my scenario. I am dynamically loading assemblies (dlls) at
runtime.
I am getting the list of types from each assembly, and if that type
implements a particular interface, then and only then do I want to
instantiate one. I have seen several articles showing how to do the
dynamic
loading, but I haven't seen anyone address this issue. Perhaps in a given
assembly, there are helper types that do not implement the interface and I
wouldn't want to instantiate one of them. How can I verify that a type
(System.Type) implements a particular interface?

Thanks.

Nov 17 '05 #2

"Joe Rattz" <jo******@yahoo.com> wrote...
How can I verify that a type
(System.Type) implements a particular interface?


If you have a Type instance (e.g. "t"), you can use:

Type t2 = t.GetInterface("ITheInterface");

If t2 is not null, then t implements ITheInterface.

// Bjorn A
Nov 17 '05 #3
Hi Joe!

You can use the "as" operator and test the result :
//***
ArrayList al = new ArrayList();
IEnumerable interf = al as IEnumerable;
if (interf == null)
Console.WriteLine("ArrayList doesn't implement IEnumerable");
else
Console.WriteLine("ArrayList implements IEnumerable");
//***

--
Best Regards
Yanick
Nov 17 '05 #4
Thanks for the input. I initially could not get this to work. It felt like
it worked backwords from what I needed, and I couldn't call IsAssignableFrom
on the interface. Since I did get Bjorn's idea to work, I initially coded it
that way. I was typing my response to your post (this response) to tell you
it wasn't working when an idea came to me. So, I used the typeof operator
on the interface and that worked. My code ended up looking like:

if(typeof(IMyInterface).IsAssignableFrom(type))
{
// instantiate an object from this type.
}

That works just fine and I prefer the code this way.

Thanks.
"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:uV**************@TK2MSFTNGP14.phx.gbl...
IsAssignableFrom

"Joe Rattz" <jo******@yahoo.com> wrote in message
news:OK**************@TK2MSFTNGP15.phx.gbl...
Ok, I am trying to figure out how to tell if a class (not object)
implements
a particular interface. Using the "is" operator does not seem to work for me and I assume its because I have a class, not an object I am wanting to check.

Here's my scenario. I am dynamically loading assemblies (dlls) at
runtime.
I am getting the list of types from each assembly, and if that type
implements a particular interface, then and only then do I want to
instantiate one. I have seen several articles showing how to do the
dynamic
loading, but I haven't seen anyone address this issue. Perhaps in a given assembly, there are helper types that do not implement the interface and I wouldn't want to instantiate one of them. How can I verify that a type
(System.Type) implements a particular interface?

Thanks.


Nov 17 '05 #5
Thanks Bjorn, that works perfect. I did code this and it did what I needed.
Ultimately though, I preferred the code to use the approach that Michael C
mentioned. Some might want to look at my response to see how this worked.

Here is what my code looked like using your approach, and it worked just
fine:

Type t = type.GetInterface("IMyInterface");
if(t != null)
{
// Instante a type object.
}

Thanks.

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

"Joe Rattz" <jo******@yahoo.com> wrote...
How can I verify that a type
(System.Type) implements a particular interface?


If you have a Type instance (e.g. "t"), you can use:

Type t2 = t.GetInterface("ITheInterface");

If t2 is not null, then t implements ITheInterface.

// Bjorn A

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cezar | last post by:
Hello, Can anyone please help me with this matter. I try to find the interfaces implemented by a class using Reflection. e.g: public class A : InterfaceB, InterfaceC how can I get (the...
3
by: New Comer | last post by:
Can somebody compare Abstract Class and Interface for me? Thanks
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
10
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
2
by: Sebastian Dau | last post by:
Hey, I'm looking for the right syntax to inherit my class from an Interface and a class at the same time: __interface ITest { };
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
3
by: Salman | last post by:
One one please tell me whats the difference between ABSTRACT Class and INTERFACE Class in c++. If you can explain me using examples, that would be more good for me.
4
by: reon | last post by:
Hi In this program i tried implements,inheritance (I tried multiple inheritance in C++). In C++ I knew that multiple inheritance means we can invoke all classes and class functions from derived...
1
by: Krish | last post by:
I have an object of a class. Now I want to know whether this class implements a particular interface or now. How do i do that. Interface ISomeInterface { } MyClass objCls= new MyClass();
11
bilibytes
by: bilibytes | last post by:
Hello, I have a little question. I would like to know how to check if an object is compliant with an interface. I know that if the Class implements explicitly the Interface, like this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
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...
0
agi2029
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.