Assembly.CreateInstance

  • Thread starter Osvaldo Bisignano
  • Start date
O

Osvaldo Bisignano

I'm using Assembly.CreateInstance (3rd overload) to create an instance of one of the types included on that dll.
But what the hell are the bindingflags? I get this exception:

MissingMethodException
Constructor not Found on class ...

Source: mscorlib
TargetSite:System.Object CreateInstanceImpl(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo, System.Object[])
STACKTRACE:
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at Legion.Comun.frmABM.EjecutarComandoDeMenu(ComandoEnum Comando) in C:\Mis Documentos\Proyectos de Visual Studio\Local\Legion\Legion.Comun\frmABM.vb:line 324

I only want to access the default constructor.

Thanks very much
 
N

Nicholas Paldino [.NET/C# MVP]

Osvaldo,

I am curious, why are you not using Activator.CreateInstance? It looks
like you want to use Assembly.CreateInstance so that you can pass parameters
to the constructor, which you can do in an easier manner with
Activator.CreateInstance.

You probably want to use the following combination of flags:

BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm using Assembly.CreateInstance (3rd overload) to create an instance of
one of the types included on that dll.
But what the hell are the bindingflags? I get this exception:

MissingMethodException
Constructor not Found on class ...

Source: mscorlib
TargetSite:System.Object CreateInstanceImpl(System.Reflection.BindingFlags,
System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo,
System.Object[])
STACKTRACE:
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder
binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean
ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args,
CultureInfo culture, Object[] activationAttributes)
at Legion.Comun.frmABM.EjecutarComandoDeMenu(ComandoEnum Comando) in
C:\Mis Documentos\Proyectos de Visual
Studio\Local\Legion\Legion.Comun\frmABM.vb:line 324

I only want to access the default constructor.

Thanks very much
 
O

Osvaldo Bisignano

It's a little complex. The type I want to instanciate is allocated in an
assembly not referenced in my application. So I receive an Assembly variable
with reflection info inside.
 
O

Osvaldo Bisignano

May the force be with you Nicholas .... :)

I see a reflection friend in you... it's good to know...
Thanks very much guy, you've really helped me
 
O

Osvaldo Bisignano

I'ts important to note that if your constructor has optional parameters (thing, I Know, shoudn't be done) you have to pass an object array with same number of elements as constructor's parameters in the args() parameters of CreateInstance
 
B

Bogdan Lachendro

It's a little complex. The type I want to instanciate is allocated in an
assembly not referenced in my application. So I receive an Assembly variable
with reflection info inside.

Could you please send the code you use when you invoke
Assembly.CreateInstance(...) ? How do you create this assembly object?
Is the type you want to instantiate public?

regards
Bogdan
 
O

Osvaldo Bisignano

of course...

thank you

Nicholas Paldino said:
Osvaldo,

Does that mean it worked? =)

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

May the force be with you Nicholas .... :)

I see a reflection friend in you... it's good to know...
Thanks very much guy, you've really helped me



Paldino said:
Osvaldo,

I am curious, why are you not using Activator.CreateInstance? It looks
like you want to use Assembly.CreateInstance so that you can pass parameters
to the constructor, which you can do in an easier manner with
Activator.CreateInstance.

You probably want to use the following combination of flags:

BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm using Assembly.CreateInstance (3rd overload) to create an instance of
one of the types included on that dll.
But what the hell are the bindingflags? I get this exception:

MissingMethodException
Constructor not Found on class ...

Source: mscorlib
TargetSite:System.Object CreateInstanceImpl(System.Reflection.BindingFlags,
System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo,
System.Object[])
STACKTRACE:
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder
binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean
ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args,
CultureInfo culture, Object[] activationAttributes)
at Legion.Comun.frmABM.EjecutarComandoDeMenu(ComandoEnum Comando) in
C:\Mis Documentos\Proyectos de Visual
Studio\Local\Legion\Legion.Comun\frmABM.vb:line 324

I only want to access the default constructor.

Thanks very much
 
N

Nicholas Paldino [.NET/C# MVP]

Osvaldo,

Does that mean it worked? =)

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

May the force be with you Nicholas .... :)

I see a reflection friend in you... it's good to know...
Thanks very much guy, you've really helped me
 

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