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. 5 16977
IsAssignableFro m
"Joe Rattz" <jo******@yahoo .com> wrote in message
news:OK******** ******@TK2MSFTN GP15.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.
"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
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.WriteLi ne("ArrayList doesn't implement IEnumerable");
else
Console.WriteLi ne("ArrayList implements IEnumerable");
//***
--
Best Regards
Yanick
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 IsAssignableFro m
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(IMyIn terface).IsAssi gnableFrom(type ))
{
// instantiate an object from this type.
}
That works just fine and I prefer the code this way.
Thanks.
"Michael C" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:uV******** ******@TK2MSFTN GP14.phx.gbl... IsAssignableFro m
"Joe Rattz" <jo******@yahoo .com> wrote in message news:OK******** ******@TK2MSFTN GP15.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.
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.GetInterfa ce("IMyInterfac e");
if(t != null)
{
// Instante a type object.
}
Thanks.
"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 info about) the "custom" interfaces InterfaceB
|
by: New Comer |
last post by:
Can somebody compare Abstract Class and Interface for me?
Thanks
|
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 be
implemented. So if you have a class which inherits base class that
inherts an interface, then your classes will have a standard. I suppose
you can also check for interface at run time say when dll is loaded and
see if it implememts whats...
|
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 class. The interface has
implied abstract methods/properties and derived classes can inherit multiple
interfaces. The interface properties/methods have no implementation.
Besides definitions of the two, what are some conceptual reasons to use...
|
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
{
};
| |
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 example:
class A
{
public:
const vector<int>& getTable() { return m_table; }
|
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.
|
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 class itself with an object...
I tied that same idea in Java , but in java it is a flop to me...
I tried inheritance using implements and interface in java...
If anybody having any idea or know how can repair this code please tell me...
Now...
|
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();
|
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:
Class RobertNestaMarley implements Smoker_Interface
the operator instanceof will let me know. like this:
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |