Invocation by reflection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to create a form by reflection. My forms' constructor takes 3 arguments so I have to use the 'long' CreateInstance overload as described below.

Assembly assembly = Assembly.Load("<myassembly>");
object oForm = assembly.CreateInstance("<myform>", true, BindingFlags.Default, null, <myctorargs>, null, null);

When running above code I get a TargetInvocationException. I DO have my assembly- and type-name correct. Is it the BindingFlags argument that causes problems? Could anybody provide me with an example of CreateInstance-arguments that would work?
 
Hi Bevo,

Could you try with the combination of the following 2 flags for the
BindingFlags enum parameter ->
BindingFlags.CreateInstance | BindingFlags.Public

Also, you may like to check to see that the order, count, type
of the Object[] args parameter (specified in
<myctorargs> in your code snippet)
matches the exact argument order, count, and type expected by the
constructor.

Regards,
Aravind C


Bevo said:
I'd like to create a form by reflection. My forms' constructor takes 3
arguments so I have to use the 'long' CreateInstance overload as described
below.
Assembly assembly = Assembly.Load("<myassembly>");
object oForm = assembly.CreateInstance("<myform>", true,
When running above code I get a TargetInvocationException. I DO have my
assembly- and type-name correct. Is it the BindingFlags argument that causes
problems? Could anybody provide me with an example of
CreateInstance-arguments that would work?
 
Bevo said:
I'd like to create a form by reflection. My forms' constructor takes 3
arguments so I have to use the 'long' CreateInstance overload as
described below.

Assembly assembly = Assembly.Load("<myassembly>");
object oForm = assembly.CreateInstance("<myform>", true,
BindingFlags.Default, null, <myctorargs>, null, null);

When running above code I get a TargetInvocationException. I DO have
my assembly- and type-name correct. Is it the BindingFlags argument
that causes problems? Could anybody provide me with an example of
CreateInstance-arguments that would work?

Please give a short but complete example showing the problem - give one
program which just creates an instance of another type, which you also
give the code to.
 
Back
Top