Activator.CreateInstance 
Author Message
 Activator.CreateInstance

Hi All,

Initially, I tried to create an object (OCX from VB) using new where the
OCX was added to the References in the Solution Explorer but I got the
compile error:
"'MyProj.MyObjClass.MyObjClass()' is inaccessible due to its protection
level"

In VB the object can be created using:
Set objMyClass = CreateObject("MyProj.MyObj");

So, I searched through this newsgroup and found that I could use the
Activator to create this object.  Someone mentioned that this was the
way to create "late bound COM objects."  I'm not sure if that's what I
have or not.

So, I'm gonna trying to use the System.Activator to create my object.
What is the best way to go about doing this that doesn't involve a lot
of messy InvokeMember stuff.  Is there a way to do this without using
InvokeMember?  So far, this is what I have...

Type typ = Type.GetTypeFromProgID("MyProj.MyObj");
object obj1 = Activator.CreateInstance(typ);

Any and all help is appreciated,

--Ed



Sun, 24 Apr 2005 08:26:05 GMT  
 Activator.CreateInstance
What you're talking about here is, the art of manually running COM and the
classfactory:

  {
  object[] param = new Object[1];
  object[] param1= new Object[1];
  object[] param2= new Object[1];
  object[] param3= new Object[1];
  object[] param4= new Object[1];
  object[] param5= new Object[1];
  object[] param6= new Object[1];
  object[] param7= new Object[1];
  string[] param8= new String[1];

  Type MyType = Type.GetTypeFromProgID("ADODB.Connection");
        object Con = System.Activator.CreateInstance(MyType);

  Type MyType1 = Type.GetTypeFromProgID("ADODB.Recordset");
  object rs = System.Activator.CreateInstance(MyType1);

        param[0] = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program
Files\\Microsoft Visual Studio\\VB98\\Nwind.mdb";
        param1[0] = 3;
        param2[0] = 3;
        param3[0] = null;
        param7[0] = "";
        param8[0] = "Provider";

  MyType.InvokeMember("ConnectionString", BindingFlags.SetProperty, null,
Con, param);
        MyType.InvokeMember("CursorLocation", BindingFlags.SetProperty,
null, Con, param1);
        MyType.InvokeMember("Mode", BindingFlags.SetProperty, null, Con,
param2);
  MyType.InvokeMember("Open", BindingFlags.SetProperty, null, Con, param3);

   MyType.InvokeMember("ConnectionString" , BindingFlags.Default |
BindingFlags.GetProperty, null, null, param7, null, null, param8);
  Console.Out.WriteLine(param8[0].ToString());
        param4[0] = param7[0].ToString();
  param5[0] = "Select * From Products";
   unchecked
   {
    MyType1.InvokeMember("Source", BindingFlags.SetProperty, null, rs,
param5);
    MyType1.InvokeMember("Open", BindingFlags.SetProperty, null, rs,
param6);

    //MessageBox.Show(rs.RecordCount.ToString());

   }
  }

HTH


Quote:
> Hi All,

> Initially, I tried to create an object (OCX from VB) using new where the
> OCX was added to the References in the Solution Explorer but I got the
> compile error:
> "'MyProj.MyObjClass.MyObjClass()' is inaccessible due to its protection
> level"

> In VB the object can be created using:
> Set objMyClass = CreateObject("MyProj.MyObj");

> So, I searched through this newsgroup and found that I could use the
> Activator to create this object.  Someone mentioned that this was the
> way to create "late bound COM objects."  I'm not sure if that's what I
> have or not.

> So, I'm gonna trying to use the System.Activator to create my object.
> What is the best way to go about doing this that doesn't involve a lot
> of messy InvokeMember stuff.  Is there a way to do this without using
> InvokeMember?  So far, this is what I have...

> Type typ = Type.GetTypeFromProgID("MyProj.MyObj");
> object obj1 = Activator.CreateInstance(typ);

> Any and all help is appreciated,

> --Ed



Sun, 24 Apr 2005 11:10:47 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Activator.CreateInstance() for OleDbConnection

2. Activator.CreateInstance

3. Activator.CreateInstance...

4. System::Activator::CreateInstance?

5. System.Activator.CreateInstance() - evidence?

6. Activator

7. Activator.CreateInstanceFrom

8. questions on AppDomains and Activators

9. BaseBundle Activator

10. Activator.GetObject VS. new

11. Activator.GetObject Method

12. Assembly.CreateInstance returns a type that generates InvalidCastException when typecasted

 

 
Powered by phpBB® Forum Software