473,785 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.LoadFr om(config.exec. assemblyName);
tRunnable = assembly.GetTyp e(config.exec.t ypeName);
object runner = assembly.Create Instance("Demo" );
Type type = runner.GetType( );
//MethodInfo mi = type.GetMethod( "Main", new Type[]{typeof(string[])});
MethodInfo mi = type.GetMethod( "Main");
Console.WriteLi ne("Have method named:{0}", mi.Name);

string[] p = new string[]{"x"};
mi.Invoke(runne r, p); //CRASH:
Unhandled Exception: System.Argument Exception: 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****** *******@manfbra un.de
(Remove the anti-spam-underscore to mail me!)
Nov 16 '05 #1
3 6312
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.LoadFr om(config.exec. assemblyName);
tRunnable = assembly.GetTyp e(config.exec.t ypeName);
object runner = assembly.Create Instance("Demo" );
Type type = runner.GetType( );
//MethodInfo mi = type.GetMethod( "Main", new Type[]{typeof(string[])});
MethodInfo mi = type.GetMethod( "Main");
Console.WriteLi ne("Have method named:{0}", mi.Name);

string[] p = new string[]{"x"};
mi.Invoke(runne r, 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.Argument Exception: 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****** *******@manfbra un.de
(Remove the anti-spam-underscore to mail me!)

Nov 16 '05 #2
Try changing this line:

mi.Invoke(runne r, 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******** ******@TK2MSFTN GP09.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.LoadFr om(config.exec. assemblyName);
tRunnable = assembly.GetTyp e(config.exec.t ypeName);
object runner = assembly.Create Instance("Demo" );
Type type = runner.GetType( );
//MethodInfo mi = type.GetMethod( "Main", new Type[]{typeof(string[])});
MethodInfo mi = type.GetMethod( "Main");
Console.WriteLi ne("Have method named:{0}", mi.Name);

string[] p = new string[]{"x"};
mi.Invoke(runne r, p); //CRASH:
Unhandled Exception: System.Argument Exception: 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****** *******@manfbra un.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******** ******@TK2MSFTN GP09.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.LoadFr om(config.exec. assemblyName);
tRunnable = assembly.GetTyp e(config.exec.t ypeName);
object runner = assembly.Create Instance("Demo" );
Type type = runner.GetType( );
//MethodInfo mi = type.GetMethod( "Main", new Type[]{typeof(string[])});
MethodInfo mi = type.GetMethod( "Main");
Console.WriteLi ne("Have method named:{0}", mi.Name);

string[] p = new string[]{"x"};
mi.Invoke(runne r, 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.Argument Exception: 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****** *******@manfbra un.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
11578
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; dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
2
13937
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, System.EventArgs e) { Assembly engine = null; AssemblyName assemblyName = new AssemblyName(); assemblyName.CodeBase = assemblyPath + @"Engine.dll";
1
1594
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 that I read from internet, I realized that InvokeMember works for methods in class, but not when I have an exe ( right ?). My program looks like this :
3
4761
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. What I would like to do is specify a class and a method which can be invoked when that file change occurs. I can specify the name of the class/method in the config file but the app will see it as a string. Therefore my question is, can I...
3
4125
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 appreciate a solution for this.. System.IO.FileNotFoundException: File or assembly name ktqipm75.dll, or one of its dependencies, was not found. File name: "ktqipm75.dll" at System.Reflection.Assembly.nLoad(AssemblyName fileName, String...
4
2007
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 implements a certain interface, I want to call that interfaces method. The problem is that the local varaible holding the user defined type is declared an object. The compiler will accept the invocation of the interface method on that object. I...
1
12701
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 pointing at a function, say func(int p1, ref int p2), how do I read p2 after a successful invocation? I've tried interogating the parameterinfo objects on the method, I've seen some hints about overriding something or casting some other object type to...
0
1725
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 Photo. Any suggestions on how to solve this? i can't do much at the moment. Server Error in '/PhotoGallery' Application. -------------------------------------------------------------------------------- Cannot create/shadow copy 'Photo' when...
0
1431
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 different assembly. For ex: Dim assemblyToLoad As Assembly = Nothing assemblyToLoad = Assembly.LoadFrom(AssemblyName)
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.