473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cannot invoke main method from assembly via reflection

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
3 6270
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: keith | last post by:
I created a class libery which has name space Assembly and class Assembly and compiled it. Then created a C# project and called a method in the external class e.g. Assembly dll;...
2
by: Martin Maat | last post by:
Hi, I am trying to create a plug-in assembly without the need to register it in the GAC. Reflection should help me out here. I got this far: private void button1_Click(object sender,...
1
by: RickDee | last post by:
Hi. I need help badly here. I need to write a program to invoke methods ( static, public, nonstatic, with parameters, and etc ) from an exe file, but I keep facing problem. From all the notes...
3
by: Simon King | last post by:
I have created a service which uses the FileSystemWatcher to monitor folders for file changes. I have a app.config file which specifies which folders to watch and what to do when a change occurs. ...
3
by: Subbu | last post by:
I have created a simple ASP.NET web server. I expose a methos helloWorld(). when I invoke this method I receive the following error message. I definitely seems like a setting issue. Would really...
4
by: anthony.wolfe | last post by:
Hello all, I'm hoping that someone could help me with this bit of code. I am using reflection to dynamically call a method within an HttpHandler. When a method returns a user defined type that...
1
by: ian | last post by:
Hi, I can't find a solution to this, so I've brought it to the experts. Using reflection I can get a MethodInfo object pointing at an assembly's method. Where I have a MethodInfo object...
0
by: Patrick F | last post by:
I keep getting this error message and i don't know whats the problem is and how to solve it. I have a webproject with 2 components (dll) added to it. One is called PhotoGallery and one is called...
0
by: pso19 | last post by:
Hi, I am trying to access all the properties, methods of an assembly using reflection. But what i want to do is invoke a method which contains custom parametrs like enumeration defined in a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.