By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Cannot invoke main method from assembly via reflection

P: n/a
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:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
See inline.

Manfred Braun wrote:
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:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

Nov 16 '05 #2

P: n/a
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

"Manfred Braun" <aa@bb.cc> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
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:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

Nov 16 '05 #3

P: n/a
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" <js***********@gmail.com> wrote in message
news:O0**************@TK2MSFTNGP09.phx.gbl...
See inline.

Manfred Braun wrote:
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:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.