Cannot invoke main method from assembly via reflection

  • Thread starter Thread starter Manfred Braun
  • Start date Start date
M

Manfred Braun

Hi All,

I am able to invoke some methods from an instance of a loaded assembly, but
I am not able to invoke the loaded assemblie's "Main" method. I try it this
way:

Assembly assembly = Assembly.LoadFrom(config.exec.assemblyName);
tRunnable = assembly.GetType(config.exec.typeName);
object runner = assembly.CreateInstance("Demo");
Type type = runner.GetType();
//MethodInfo mi = type.GetMethod("Main", new Type[]{typeof(string[])});
MethodInfo mi = type.GetMethod("Main");
Console.WriteLine("Have method named:{0}", mi.Name);

string[] p = new string[]{"x"};
mi.Invoke(runner, p); //CRASH:
Unhandled Exception: System.ArgumentException: Object type cannot be
converted to target type.

the Main method of the loaded assembly is:

public static void Main(string[] args)
{
Demo d = new Demo();
d.Init("m1", args);
d.Run();
}

I am able to invoke other (instance!!!-)methods of the assembly!
Any thoughts, tips, hints or pointer to further information are really very
welcomed!!!!

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
See inline.

Manfred said:
Hi All,

I am able to invoke some methods from an instance of a loaded assembly, but
I am not able to invoke the loaded assemblie's "Main" method. I try it this
way:

Assembly assembly = Assembly.LoadFrom(config.exec.assemblyName);
tRunnable = assembly.GetType(config.exec.typeName);
object runner = assembly.CreateInstance("Demo");
Type type = runner.GetType();
//MethodInfo mi = type.GetMethod("Main", new Type[]{typeof(string[])});
MethodInfo mi = type.GetMethod("Main");
Console.WriteLine("Have method named:{0}", mi.Name);

string[] p = new string[]{"x"};
mi.Invoke(runner, p); //CRASH:

[There are two issues in this line:
1> To invoke a static method using its MethodInfo object, the first
parameter should be a null reference (Nothing)
2> The invoke method takes an object array, which is an argument list
for the invoked method. In your case, the method itself takes an array
as it's argument. So, you need to do this way:

object[] p=new object[] {new string[]{"x"} };

HTH

Jianwei


Unhandled Exception: System.ArgumentException: Object type cannot be
converted to target type.

the Main method of the loaded assembly is:

public static void Main(string[] args)
{
Demo d = new Demo();
d.Init("m1", args);
d.Run();
}

I am able to invoke other (instance!!!-)methods of the assembly!
Any thoughts, tips, hints or pointer to further information are really very
welcomed!!!!

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
Try changing this line:

mi.Invoke(runner, p);

to this:

mi.Invoke(null, p);

(You're invoking a static method, so there's no object instance to invoke it
on).

Jim
 
Hello Jon and All,

thanks a lot!!!! THIS is the complete solution !!!! I have to learn a lot
about reflection, I would never be able to solve this ...... calling my
other methods, also with arguments, was easy ;-)

Much thanks and
best regards,
Manfred

John Sun said:
See inline.

Manfred said:
Hi All,

I am able to invoke some methods from an instance of a loaded assembly, but
I am not able to invoke the loaded assemblie's "Main" method. I try it this
way:

Assembly assembly = Assembly.LoadFrom(config.exec.assemblyName);
tRunnable = assembly.GetType(config.exec.typeName);
object runner = assembly.CreateInstance("Demo");
Type type = runner.GetType();
//MethodInfo mi = type.GetMethod("Main", new Type[]{typeof(string[])});
MethodInfo mi = type.GetMethod("Main");
Console.WriteLine("Have method named:{0}", mi.Name);

string[] p = new string[]{"x"};
mi.Invoke(runner, p); //CRASH:

[There are two issues in this line:
1> To invoke a static method using its MethodInfo object, the first
parameter should be a null reference (Nothing)
2> The invoke method takes an object array, which is an argument list
for the invoked method. In your case, the method itself takes an array
as it's argument. So, you need to do this way:

object[] p=new object[] {new string[]{"x"} };

HTH

Jianwei


Unhandled Exception: System.ArgumentException: Object type cannot be
converted to target type.

the Main method of the loaded assembly is:

public static void Main(string[] args)
{
Demo d = new Demo();
d.Init("m1", args);
d.Run();
}

I am able to invoke other (instance!!!-)methods of the assembly!
Any thoughts, tips, hints or pointer to further information are really very
welcomed!!!!

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
Back
Top