Doubt in Activator.CreateInstance

  • Thread starter Thread starter Baskar RajaSekharan
  • Start date Start date
B

Baskar RajaSekharan

Hi I have one doubt in C#.

I created one component called CustomTesting in VC7. The DLL
contains Two Method called Execute and SetOCxInterface. I want to Load the
Dll
from my Sample application(which is in C#) with out Addeing it in
Reference. ie i want to load it dynamically with the help of
Activator.CreateInstance. Then i want to access those method. How to Do
that?

onject inst;
Type myType1= Type.GetTypeFromProgID("CustomTesting.CallbackImpl.1");
inst = Activator.CreateInstance(myType1);

When i say inst . the intellicence is not showing the above Two methods
(Execute and SEtOCxInterface). For that i have to cast to the Default
interface .. correct?
TO cast it i hve to include it in a Namespace list. Since it is a
dynamically loaded dll , how can i added into namespace list dyna,ically?
Plese send a solution and give me the dample code.

Note I am herewith sending the VC& Code(which the component is created).


Regards,
R.Baskar
 
Baskar RajaSekharan said:
Hi I have one doubt in C#.

I created one component called CustomTesting in VC7. The DLL
contains Two Method called Execute and SetOCxInterface. I want to Load the
Dll
from my Sample application(which is in C#) with out Addeing it in
Reference. ie i want to load it dynamically with the help of
Activator.CreateInstance. Then i want to access those method. How to Do
that?

onject inst;
Type myType1= Type.GetTypeFromProgID("CustomTesting.CallbackImpl.1");
inst = Activator.CreateInstance(myType1);

When i say inst . the intellicence is not showing the above Two methods
(Execute and SEtOCxInterface). For that i have to cast to the Default
interface .. correct?
TO cast it i hve to include it in a Namespace list. Since it is a
dynamically loaded dll , how can i added into namespace list dyna,ically?
Plese send a solution and give me the dample code.

Note I am herewith sending the VC& Code(which the component is created).


Regards,
R.Baskar

Activator.CreateInstance returns an "Object", so it has no (useful)
properties/methods.
You need to cast it into something you can use.

The way this usually works (correct me if I'm wrong) is that you have a
base-class
or interface that you know how to work with. All created instances can be
cast to that
specific class (or interface), so then you (and Intellisense) know what
properties/methods
are available.

Using reflection it is probably possible to use any method, but this is more
complicated
(read: I don't know (yet) how to do it :-)


Hans Kesting
 
Hans Kesting said:
Using reflection it is probably possible to use any method, but this is more
complicated
(read: I don't know (yet) how to do it :-)

No, it is not, practically.

For any sort of "add in mechanism" to work, the using class (loading the add
in) has to KNOW how the add in will look and behave.

Like a contract.

So, while reflection would help to find all methods, you still could only
use the ones which you expect.

And then, an interface or base class is just way more convenient to use.

Simply because basically the only thing you can do with all the other
methods is - ignore them.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Back
Top