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.
quasar_eeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.