Activator.CreateInstance not able to create object in .net console application

sabinofz

New member
Joined
Dec 22, 2024
Messages
3
Programming Experience
10+
I have a solution with several vb projects and one console application in .net
In console application y have references as projects the vb projects.
When i run the console application and I execute :

C#:
aAccion = Activator.CreateInstance(Type.GetType("BP.CN.MPIion" + sTokens(0)), oParameters)

I get an error that console application is not able to create the object. The dll that manage
this object in in a the vb projects referenced.

I checked if the dll is loaded when i run console application and is not loaded.
I loaded manyally the dell with code in init of console application and it doesn´t work also.
I tried to add dlls directrly as referenced and it doesn´t work also.
Any idea or solution?
 
Last edited by a moderator:
Please post the exact error message you are getting.

It's usually better to break-up your code into multiple lines so that you can potentially see what the issue is. So you should have something like:
C#:
var typeName = "BP.CN.MPIion" + sTokens(0);
var type = Type.GetType(typeName);
aAccion = Activator.CreateInstance(type, oParameters);

You can then step through your code with a debugger to see the values along each stage of the way, as well as identify which step is failing. If I were to guess, the Type.GetType() is returning null. In my mind, this is partly confirmed by your report that the DLL is not loaded.

Don't be tricked into thinking that less lines is more efficient. The compiler is very good at folding code when building release builds.

Not related to your problem: Don't use Hungarian notation when writing C# code. This is stated Microsoft's naming conventions.
 
Last edited:
Looks like your question was closed in StackOverflow because you didn't do enough to provide debugging information. You'll not get much engagement here either without sufficient information.
 
Please post the exact error message you are getting.

It's usually better to break-up your code into multiple lines so that you can potentially see what the issue is. So you should have something like:
C#:
var typeName = "BP.CN.MPIion" + sTokens(0);
var type = Type.GetType(typeName);
aAccion = Activator.CreateInstance(type, oParameters);

You can then step through your code with a debugger to see the values along each stage of the way, as well as identify which step is failing. If I were to guess, the Type.GetType() is returning null. In my mind, this is partly confirmed by your report that the DLL is not loaded.

Don't be tricked into thinking that less lines is more efficient. The compiler is very good at folding code when building release builds.

Not related to your problem: Don't use Hungarian notation when writing C# code. This is stated Microsoft's naming conventions.

And what solution suggest?
 
Please post the exact error message you are getting.

It's usually better to break-up your code into multiple lines so that you can potentially see what the issue is. So you should have something like:
C#:
var typeName = "BP.CN.MPIion" + sTokens(0);
var type = Type.GetType(typeName);
aAccion = Activator.CreateInstance(type, oParameters);

You can then step through your code with a debugger to see the values along each stage of the way, as well as identify which step is failing. If I were to guess, the Type.GetType() is returning null. In my mind, this is partly confirmed by your report that the DLL is not loaded.

Don't be tricked into thinking that less lines is more efficient. The compiler is very good at folding code when building release builds.

Not related to your problem: Don't use Hungarian notation when writing C# code. This is stated Microsoft's naming conventions.

1734893360120.png

Here the error
 
Considering that the error was all text, it would have been better to paste it as text to make it much easier for your readers, specially for people trying to follow along on their phones.

Anyway, here's what I managed to get:
Code:
El log de la ejecución se guardó en: C:\VS2010\BPM\BPMSGRA\BPMSGRA.AnonimizarDatosSGRA\bin\Debug\net8.0\Log_BPMSGRA.AnonimizarDatosSGRA.txt
Ocurrio un error al inicializar el proceso o al preparar el archivo de log:
Error: Exception has been thrown by the target of an invocation.
at System. RuntimeType. CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at System. RuntimeType. CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) 
at System.Activator.CreateInstance(Type type, Object[] args)
at BPMSGRA.CN.MPICDW.ModeloProceso_MPICDW.CrearAccion(PersistenciaAccion paPersistenciaAccion, Accion& aAccion) in C:\VS2010\BPM\BPMSGRA\BPMSGRA.CN.MPICDW\odeloProceso_MPICDW.vb:line 125
at BPM.CN.CNBPM.CNBPM. IMPINT_LeerAcciones(FiltroAccion faFiltroAccion, Proceso pProceso) in C:\VS2010\BPM\BPM\BPM.CN.CNBPM\CNBPM_Accion.vb:line 493
at BPM.CN.CNBPM.CNBPM. LeerProcesoCompleto(Proceso pProceso, FiltroProceso fpFiltroProceso) in C:\VS2010\BPM\BPM\BPM.CN.CNBPM\CNBPM_Proceso.vb:line 515
at BPM.CN.CNBPM.CNBPM.LeerProcesos(FiltroProceso fpFiltroProceso) in C:\VS2010\BPM\BPM\BPM.CN.CNBPM\CNBPM_Proceso.vb:line 39
at BPMSGRA.CN.CNBPMSGRA.CNBPMSGRA.LeerProcesosBPMSGRA(FiltroProcesoBPMSGRA fpFiltroProceso) in C:\VS2010\BPM\BPMSGRA\BPMSGRA.CN.CNBPMSGRA\CNBPMSGRA.vb:line 66
at BPMSGRA.CS.CSBPMSGRA.CSBPMSGRA.LeerProcesos(FiltroProcesoBPMSGRA fpFiltroProceso) in C:\VS2010\BPM\BPMSGRA\BPMSGRA.CS.CSBPMSGRA\CSBPMSGRA.vb:line 186
at BPMSGRA.AnonimizarDatosSGRA.AnonimizarDatosSGRA.LeerProcesos() in C:\VS2010\BPM\BPMSGRA\BPMSGRA.AnonimizarDatosSGRA\AnonimizarDatosSGRA.cs:line 323
at BPMSGRA. AnonimizarDatosSGRA.AnonimizarDatosSGRA.ejecutarAnonimizadorDatosSGRA() in C:\VS2010\BPM\BPMSGRA\BPMSGRA.AnonimizarDatosSGRA\AnonimizarDatosSGRA.cs:line 203

Based on what I can see, the exception is not happening within your C# code, but rather within your VB code. Specifically:
C#:
C:\VS2010\BPM\BPMSGRA\BPMSGRA.CN.MPICDW\odeloProceso_MPICDW.vb:line 125

This is a C# forum, not a VB forum. Perhaps you want to post your question in the sister form: VB.NET Developer Community ?

But again, you'll need to provide more information like:
what is the value of "BP.CN.MPIion" + sTokens(0)?
what is the result of Type.GetType("BP.CN.MPIion" + sTokens(0))?
 
Back
Top Bottom