How to instantiate a class?

C

CSharper

I have written a class, which reads a config file, which has the name
of the dll, the type of the class. Which during running, I use
reflection to identify and then instantiate the class. I expect all
the dll to implement an interface. I was able to make the program to
load the assembly, but when I try to instantiate to my interface I get
type mismatch error. One thing is that, the dlls were written by
different group and they use differenet name space and my interface
use different name space. Will that be an issue?

Thanks,
 
P

Paul

Nice answer peter thats brought a smile to my face.

Developers love their Copy >> Paste
 
P

Paul

Nice answer peter thats brought a smile to my face.

Developers love their Copy >> Paste
 
C

CSharper

As long as they implement your own interface, and not simply some  
copied-and-pasted duplicate of your interface, no problem.  To do that, 
you need to provide your own DLL containing the interface, which they will  
then reference in their project.

As far as the specific question goes: are you trying to instantiate your  
interface?  Or their class?  You _must_ do the latter.  You can't  
instantiate an interface itself.

To instantiate the proper class, you need to use reflection to search for 
the type(s) in their DLL that implement(s) your interface, and then  
instantiate that type (e.g. using Activator).

Pete

Hi Pete,

Thank you very much for the detailed answer. Now I understand why I
can not instantiate. I am wondering, I know for sure they have an
implementation of 2 o3 operation in their types (for sake of
conversation). Now I know also what is their type, I can create an
object as
object obj = Activator.CreateInstance(classType);
How would go about calling a particular method from this obj?
What I am getting at is, I do not want to distribute my dll to all the
groups they are doing dll development.
Thanks,
 
C

CSharper

As long as they implement your own interface, and not simply some  
copied-and-pasted duplicate of your interface, no problem.  To do that, 
you need to provide your own DLL containing the interface, which they will  
then reference in their project.

As far as the specific question goes: are you trying to instantiate your  
interface?  Or their class?  You _must_ do the latter.  You can't  
instantiate an interface itself.

To instantiate the proper class, you need to use reflection to search for 
the type(s) in their DLL that implement(s) your interface, and then  
instantiate that type (e.g. using Activator).

Pete

Hi Pete,

Thank you very much for the detailed answer. Now I understand why I
can not instantiate. I am wondering, I know for sure they have an
implementation of 2 o3 operation in their types (for sake of
conversation). Now I know also what is their type, I can create an
object as
object obj = Activator.CreateInstance(classType);
How would go about calling a particular method from this obj?
What I am getting at is, I do not want to distribute my dll to all the
groups they are doing dll development.
Thanks,
 
C

CSharper

Hi Pete,

Thank you very much for the detailed answer. Now I understand why I
can not instantiate. I am wondering, I know for sure they have an
implementation of 2 o3 operation in their types (for sake of
conversation).  Now I know also what is their type, I can create an
object as
object obj = Activator.CreateInstance(classType);
How would go about calling a particular method from this obj?
What I am getting at is, I do not want to distribute my dll to all the
groups they are doing dll development.
Thanks,- Hide quoted text -

- Show quoted text -

I was able to call a method by using MethodInfo property. Just
curious, is there any other way?
Thanks,
 
C

CSharper

Hi Pete,

Thank you very much for the detailed answer. Now I understand why I
can not instantiate. I am wondering, I know for sure they have an
implementation of 2 o3 operation in their types (for sake of
conversation).  Now I know also what is their type, I can create an
object as
object obj = Activator.CreateInstance(classType);
How would go about calling a particular method from this obj?
What I am getting at is, I do not want to distribute my dll to all the
groups they are doing dll development.
Thanks,- Hide quoted text -

- Show quoted text -

I was able to call a method by using MethodInfo property. Just
curious, is there any other way?
Thanks,
 
C

CSharper

[...]
How would go about calling a particular method from this obj?
What I am getting at is, I do not want to distribute my dll to all the
groups they are doing dll development.
Thanks,- Hide quoted text -
- Show quoted text -
I was able to call a method by using MethodInfo property. Just
curious, is there any other way?

To do it any other way, your code needs to be compiled with the _actual_  
type that they've implemented.  For that to happen, their code needs tobe  
compiled with your DLL, or they need to provide you a DLL that you can  
reference in your code that declares the type that you'll be using (which,  
for multiple external DLLs for you to support of course means you need to 
compile your code with multiple DLLs, or they all need to get together and  
share a common DLL that declares an interface you'll use).

Otherwise, straight reflection invocation is pretty much it.

I don't understand your reluctance to distribute a DLL to them.  It is the  
most obvious approach to the problem.  It's a technique used by a wide  
variety of people in a wide variety of situations, and works quite well.  
Note that you don't need to deliver to them a DLL with your entire  
implementation.  All that's required is a DLL your code and theirs both 
can use, in which the interface(s) of interest is(are) declared.

Pete

Thanks Pete, I was just picking your brain for any good ideas. :)
I am also looking at System.Addin
 
C

CSharper

[...]
How would go about calling a particular method from this obj?
What I am getting at is, I do not want to distribute my dll to all the
groups they are doing dll development.
Thanks,- Hide quoted text -
- Show quoted text -
I was able to call a method by using MethodInfo property. Just
curious, is there any other way?

To do it any other way, your code needs to be compiled with the _actual_  
type that they've implemented.  For that to happen, their code needs tobe  
compiled with your DLL, or they need to provide you a DLL that you can  
reference in your code that declares the type that you'll be using (which,  
for multiple external DLLs for you to support of course means you need to 
compile your code with multiple DLLs, or they all need to get together and  
share a common DLL that declares an interface you'll use).

Otherwise, straight reflection invocation is pretty much it.

I don't understand your reluctance to distribute a DLL to them.  It is the  
most obvious approach to the problem.  It's a technique used by a wide  
variety of people in a wide variety of situations, and works quite well.  
Note that you don't need to deliver to them a DLL with your entire  
implementation.  All that's required is a DLL your code and theirs both 
can use, in which the interface(s) of interest is(are) declared.

Pete

Thanks Pete, I was just picking your brain for any good ideas. :)
I am also looking at System.Addin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top