-4

Quoted from this site (click):

Main is declared inside a class or struct. Main must be static and it should not be public.

It means that the Main method can be public.

Now consider the following 2 projects:

Project 1: HelloWorld

using static System.Console;

namespace HelloWorld
{
    class Program
    {
        public static void Main()
        {
            WriteLine("Hello World");
        }
    }
}

I assign public modifier to the Main. Then I compile it to produce an application named HelloWorld.exe.

Project 2: Invoker

namespace Invoker
{
    class Program
    {
        static void Main()
        {
            // I want to invoke Main in HelloWorld.exe here.
            // How to do it?
        }
    }
}

This project tries to invoke Main defined in HelloWorld.exe

Question

Without using Process, how can the Invoker.exe call public Main defined in HelloWorld.exe? This question is used to find an example to invoke public Main from outside. That is why I don't want to use Process.

  • 2
    possible duplicate of [What is the difference between public static void Main() and private static void Main() in a C# console application?](http://stackoverflow.com/questions/21871104/what-is-the-difference-between-public-static-void-main-and-private-static-void) – BoltClock Oct 24 '15 at 04:36
  • 2
    This question's unclear. Are you asking simply if such an example exists? Or are you asking for a specific example for your own use? Calling a `public static void Main()` method in your `Program` class is no different than calling any other public, static method: add a reference to your assembly (if using from a different one), and then call it as e.g. `Program.Main();` (or if you have `string[] args`, passing an appropriate value of course). The proposed duplicate doesn't actually include any useful code examples, so it doesn't seem to address the question you are asking, whatever that is. – Peter Duniho Oct 24 '15 at 05:04
  • `AppDomain.CurrentDomain.ExecuteAssembly("HelloWorld.exe")` – user4003407 Oct 24 '15 at 17:46

1 Answers1

2

Main should not be public since it is not supposed to be called by any other methods in your assemblies. It is only the CLR that should be able to call it when the application starts. The access specifiers does not apply to the CLR in the same way (if at all) as to your assemblies that are managed by the CLR.

If you want it to be public; but that would mean any other application could reference it without reflection.

That's what the runtime does; just start looking for static methods named "Main" with various signatures and then invokes the first one it finds. Likely something like this:

foreach(Type type in assembly.GetTypes())
{
    foreach(MethodInfo methodInfo in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
    {
        if (methodInfo.Name == "Main" /*TODO: && methodInfo.Parameters is valid*/)
        {
            methodInfo.Invoke(null, new object[0]);
        }
    }
}
Tummala Krishna Kishore
  • 8,140
  • 4
  • 34
  • 47