CreateObject in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john

    #1

    CreateObject in C#

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

    Dim oDataLinks As Object
    oDataLinks = CreateObject("D ataLinks")

    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
  • Rob Windsor

    #2
    Re: CreateObject in C#

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

    Type t = System.Type.Get TypeFromProgID( "Word.Applicati on");
    Object o = Activator.Creat eInstance(t);
    t.InvokeMember( "Visible", BindingFlags.Se tProperty, null, o, new object[]
    {true});
    MessageBox.Show ("Word Started");
    t.InvokeMember( "Quit", BindingFlags.In vokeMethod, null, o, new object[] {});
    MessageBox.Show ("Word Done");

    Hope this helps.

    --
    Rob Windsor
    G6 Consulting
    Toronto, Canada



    "john" <john@john.co m> wrote in message
    news:098301c37e f9$aafefd20$a10 1280a@phx.gbl.. .[color=blue]
    > In VS.Net you can use the CreateObject function to create
    > a late bound COM object. For example:
    >
    > Dim oDataLinks As Object
    > oDataLinks = CreateObject("D ataLinks")
    >
    > 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[/color]


    Comment

    • Rob Windsor

      #3
      Re: CreateObject in C#

      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.Reflecti on;
      using System.Windows. Forms;

      Rob

      "Rob Windsor" <rwindsor@NO.MO RE.SPAM.bigfoot .com> wrote in message
      news:OLHdnwvfDH A.956@TK2MSFTNG P09.phx.gbl...[color=blue]
      > It can be done but it's pretty nasty code. Here's an simple example of[/color]
      using[color=blue]
      > Word.
      >
      > Type t = System.Type.Get TypeFromProgID( "Word.Applicati on");
      > Object o = Activator.Creat eInstance(t);
      > t.InvokeMember( "Visible", BindingFlags.Se tProperty, null, o, new object[]
      > {true});
      > MessageBox.Show ("Word Started");
      > t.InvokeMember( "Quit", BindingFlags.In vokeMethod, null, o, new object[][/color]
      {});[color=blue]
      > MessageBox.Show ("Word Done");
      >
      > Hope this helps.
      >
      > --
      > Rob Windsor
      > G6 Consulting
      > Toronto, Canada
      >
      >
      >
      > "john" <john@john.co m> wrote in message
      > news:098301c37e f9$aafefd20$a10 1280a@phx.gbl.. .[color=green]
      > > In VS.Net you can use the CreateObject function to create
      > > a late bound COM object. For example:
      > >
      > > Dim oDataLinks As Object
      > > oDataLinks = CreateObject("D ataLinks")
      > >
      > > 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[/color]
      >
      >[/color]


      Comment

      Working...