Problem with Activator.CreateInstance in C#

D

Doug Riley

I am using CreateInstance to create an instance of a class and invoke
a function of that class. I really need it to execute in a single
line of code (long story, but I want to execute this code in the
command window on demand, without declaring any variables).

This code WORKS (but requires 2 lines of code and a variable
declaration):
Type typ = Type.GetTypeFromProgID("MyDLLx.clsMyClass");
Activator.CreateInstance(typ).mNoShow("Test");

This Code DOES NOT WORK:
Activator.CreateInstance((System.Type)Type.GetTypeFromProgID("MyDLLx.clsMyClass")))).mNoShow("Test");

The error, when executed from the command window is:
arguments do not match parameters for function ...

The error (compile), when coded in the application is:
\'object' does not contain a definition for 'mNoShow'

Any help is most appreciated.
 
D

Daniel O'Connell [C# MVP]

Type typ = Type.GetTypeFromProgID("MyDLLx.clsMyClass");
Activator.CreateInstance(typ).mNoShow("Test");

This Code DOES NOT WORK:
Activator.CreateInstance((System.Type)Type.GetTypeFromProgID("MyDLLx.clsMyClass")))).mNoShow("Test");

The error, when executed from the command window is:
arguments do not match parameters for function ...

The error (compile), when coded in the application is:
\'object' does not contain a definition for 'mNoShow'

Any help is most appreciated.

Are you sure these are verbatim lines? Neither one will work.(Although it
probably will in VB)
 
N

Nicholas Paldino [.NET/C# MVP]

Doug,

Actually, neither line of code should work. The reason for this is that
the CreateInstance method returns an object. The only way I can see this
working is if it is in VB.

I would recheck the two lines you say that work, because they shouldn't.

Is it possible that the two lines are in a block that is swallowing the
exception?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top