• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

how to call main method in c sharp?

How can I call main method from a method defined in some other class/library?
Ideally with all existing args[].

Thanks in advance.
0
quasar_ee
Asked:
quasar_ee
  • 2
2 Solutions
 
krunal_shahCommented:
you just need to create the instnace of the class and call the metod by passing the arguments reqired.
like,
if you have created a method named TestMethod in side TestClass then you can call it like,
 TestClass objclass = new TestClass();
 objclass.TestMethod("1", "2");
0
 
lazyberezovskyCommented:
Main method is the static method. By default with one of following signatures:
static void Main()
static int Main()
static void Main(string[] args)
static int Main(string[] args)

And you don't need class instance for invocation that method.
Try something like this:

Assembly assembly = Assembly.LoadFrom("ConsoleApplication1.exe");
Type type = assembly.GetType("ConsoleApplication1.Program");
string[] parameter = {"arg1", "arg2" };
type.InvokeMember("Main", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, type, new object[] { parameter });

Open in new window

0
 
lazyberezovskyCommented:
BTW consider using System.Diagnostics.Process for creating instance of other application (one with Maing method):
ProcessStartInfo info = new ProcessStartInfo("ConsoleApplication1.exe", "arg1 arg2");
//info.CreateNoWindow = true;
Process.Start(info);

Open in new window

0
 
quasar_eeAuthor Commented:
ok
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now