473,396 Members | 1,995 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,396 software developers and data experts.

Call a Main function from other Main function

In my console application I have a class and a main function to it as
Expand|Select|Wrap|Line Numbers
  1. namespace Console1
  2. {
  3.    Public class Program1
  4.             {
  5.                    static void Main (string[] args)
  6.                         {
  7.                              Console.WriteLine("Hai");
  8.                          }
  9.             }
  10. }
  11.  
I have created another new application and the namespace is Console2 and the class name is Program2, I have reffered this to my Console 2 by the following way

Expand|Select|Wrap|Line Numbers
  1. namespace Console2
  2. {
  3.    using Console2;
  4.    Public class Program2
  5.             {
  6.                    static void Main (string[] args)
  7.                         {
  8.                         }
  9.             }
  10. }
  11.  
Now if I need to get the Main function of Program1 in Program2 how should I do that?
Thanks
Raghul
Oct 14 '08 #1
4 1291
akshaycjoshi
153 100+
Then use Assembly.LoadFrom(assemblypath) method to load that assemly.
Then obtain the type from that assemly.
Then obtain the MethodInfo object and use methodInfo.Invoke().
Oct 14 '08 #2
Then use Assembly.LoadFrom(assemblypath) method to load that assemly.
Then obtain the type from that assemly.
Then obtain the MethodInfo object and use methodInfo.Invoke().
I added the reference of console1 with that of Console2 and I implemented the concept of reflection as you told.
Expand|Select|Wrap|Line Numbers
  1. namespace Console2 
  2.    using Console1; 
  3.    Public class Program2 
  4.             { 
  5.                    static void Main (string[] args) 
  6.                         { 
  7.              Assembly sampleassembly;
  8.             sampleassembly = Assembly.LoadFrom("ConsoleApplication2");
  9.             MethodInfo mi = sampleassembly.GetTypes()[0].GetMethod("Main");
  10.                         } 
  11.             } 
  12.  
here it shows an error as file not found what has to be done?
is there any way ?
thanks in advance
Raghul
Oct 14 '08 #3
akshaycjoshi
153 100+
First u dont need to add reference to the assembly nor you need to keep it in the bin directory as long as u use Assembly.LoadFrom().

Expand|Select|Wrap|Line Numbers
  1. Assembly asm = Assembly.LoadFrom(@"C:\Documents and  Settings\Administrator\My Documents\Visual Studio 2005\Projects\firstmain\firstmain\bin\Debug\firstmain.dll");
Now assembly is loaded but u have to get a reference to the class cantaining the Main() method.
Do this as

Expand|Select|Wrap|Line Numbers
  1. Type t = asm.GetType( [fully qualified name of the class cantaining the method Main] ,true, true);
Now we have reference to the class.Then you need to point to the specific method in the class.
Do this as

Expand|Select|Wrap|Line Numbers
  1. MethodInfo m = t.GetMethod("Main", BindingFlags.Static | BindingFlags.Public);
Those binding flags specify what kind of method u wanna search.
If ur Main() is public and static use the above combination. (BindingFlags.Static | BindingFlags.Public)

Now you need to invoke the function.
Do this as
Expand|Select|Wrap|Line Numbers
  1. m.Invoke(null, null);
the first argument is for object(since it's static method u wanna access use null).Second argument specifies the arguments(again kept null).

Hope it helps !
Oct 14 '08 #4
First u dont need to add reference to the assembly nor you need to keep it in the bin directory as long as u use Assembly.LoadFrom().

Expand|Select|Wrap|Line Numbers
  1. Assembly asm = Assembly.LoadFrom(@"C:\Documents and  Settings\Administrator\My Documents\Visual Studio 2005\Projects\firstmain\firstmain\bin\Debug\firstmain.dll");
Now assembly is loaded but u have to get a reference to the class cantaining the Main() method.
Do this as

Expand|Select|Wrap|Line Numbers
  1. Type t = asm.GetType( [fully qualified name of the class cantaining the method Main] ,true, true);
Now we have reference to the class.Then you need to point to the specific method in the class.
Do this as

Expand|Select|Wrap|Line Numbers
  1. MethodInfo m = t.GetMethod("Main", BindingFlags.Static | BindingFlags.Public);
Those binding flags specify what kind of method u wanna search.
If ur Main() is public and static use the above combination. (BindingFlags.Static | BindingFlags.Public)

Now you need to invoke the function.
Do this as
Expand|Select|Wrap|Line Numbers
  1. m.Invoke(null, null);
the first argument is for object(since it's static method u wanna access use null).Second argument specifies the arguments(again kept null).

Hope it helps !
thanks a lot I got it!
Oct 15 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Charles Soto | last post by:
I've got a main loop script that calls two other scripts that do no user interaction. All they do is send a couple of mysql update statements. Then they use header() to call the main loop again. ...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
17
by: Sara | last post by:
Hi, I'm having a problem with my program and I think it stems from me not understand how to call a function and return a int value to main. What I have to do is create a program that runs...
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
11
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.