CreateObject in C#

  • Thread starter Thread starter john
  • Start date Start date
J

john

In VS.Net you can use the CreateObject function to create
a late bound COM object. For example:

Dim oDataLinks As Object
oDataLinks = CreateObject("DataLinks")

This creates a DataLinks object that can be used to get a
connection object. Its easily done in VB.Net.

But what do you use the CreateObject function in C#? is
there an equivilant?

thanks
 
It can be done but it's pretty nasty code. Here's an simple example of using
Word.

Type t = System.Type.GetTypeFromProgID("Word.Application");
Object o = Activator.CreateInstance(t);
t.InvokeMember("Visible", BindingFlags.SetProperty, null, o, new object[]
{true});
MessageBox.Show("Word Started");
t.InvokeMember("Quit", BindingFlags.InvokeMethod, null, o, new object[] {});
MessageBox.Show("Word Done");

Hope this helps.
 
Oooops, I forgot to metion you'll need a couple using statements to make the
code from my last post work.

using System;
using System.Reflection;
using System.Windows.Forms;

Rob

Rob Windsor said:
It can be done but it's pretty nasty code. Here's an simple example of using
Word.

Type t = System.Type.GetTypeFromProgID("Word.Application");
Object o = Activator.CreateInstance(t);
t.InvokeMember("Visible", BindingFlags.SetProperty, null, o, new object[]
{true});
MessageBox.Show("Word Started");
t.InvokeMember("Quit", BindingFlags.InvokeMethod, null, o, new object[] {});
MessageBox.Show("Word Done");

Hope this helps.

--
Rob Windsor
G6 Consulting
Toronto, Canada



john said:
In VS.Net you can use the CreateObject function to create
a late bound COM object. For example:

Dim oDataLinks As Object
oDataLinks = CreateObject("DataLinks")

This creates a DataLinks object that can be used to get a
connection object. Its easily done in VB.Net.

But what do you use the CreateObject function in C#? is
there an equivilant?

thanks
 
Back
Top