C# equivalent of (old-style) VB's CreateObject(progId) ?

  • Thread starter Thread starter Gary McGill
  • Start date Start date
G

Gary McGill

In (traditional) VB, it's possible to instantiate a COM object using
something like Set obj = CreateObject("MyDll.MyObject), and then call that
object's methods (either using late binding, or using early binding if the
"obj" variable is declared as an object of a particular type).

I'd like to do something similar in C#, but I'm not sure whether this can be
done?

To illustrate what I'm trying to do, suppose I'm defining an interface for
screen-saver plug-ins. I have a core program that's capable of calling a
plug-in supporting that interface, but I don't want that program to have to
know in advance what plug-ins are available. When I install a new plug-in, I
want to be able to make that know to my program simply by adding the details
of the new plug-in to a configuration file. Those details would (in the
old-style COM world) contain something like the prog ID of the new plug-in.

In the COM/VB world, I would do something like this:

Dim oPlugin As IPlugIn
Set oPlugin = CreateObject(strProgId)

By declaring the oPlugin variable using the name of the interface that all
plug-ins support (IPlugin), I get some of the benefit of early binding (such
as intellisense), coupled with the ability to bind to any as-yet-unspecified
implementation via the prog ID.

In the C# world I know how to declare and implement an interface, but I have
no idea how I would actually instantiate a class from a different assembly
that did not exist at the time my program was compiled, and whose name I
just read from a configuration file?

Can it be done? Easily?

Thanks in advance,
Gary McGill
 
Look at the Activator.CreateInstance method - it works great with interface based invocation.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

In (traditional) VB, it's possible to instantiate a COM object using
something like Set obj = CreateObject("MyDll.MyObject), and then call that
object's methods (either using late binding, or using early binding if the
"obj" variable is declared as an object of a particular type).

I'd like to do something similar in C#, but I'm not sure whether this can be
done?

To illustrate what I'm trying to do, suppose I'm defining an interface for
screen-saver plug-ins. I have a core program that's capable of calling a
plug-in supporting that interface, but I don't want that program to have to
know in advance what plug-ins are available. When I install a new plug-in, I
want to be able to make that know to my program simply by adding the details
of the new plug-in to a configuration file. Those details would (in the
old-style COM world) contain something like the prog ID of the new plug-in.
 
I'm not looking to getting flamed here... but if you're lazy or until
you figure this out you can always directly use
"Interaction.CreateObject" by referencing Microsoft.VisualBasic in
your C# project.

I'm not endorsing this - just letting you know it's an option.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
Back
Top