-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.

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • 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]);
        }
    }
}
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
  • Could you kindly revisit my question and update your answer to conform to my question? – Second Person Shooter Oct 24 '15 at 15:51
  • @YasashiiEirian please refer this [link](http://stackoverflow.com/questions/1112981/how-do-i-launch-application-one-from-another-in-c) – Krsna Kishore Oct 24 '15 at 15:55
  • No. I don't want to use Process. – Second Person Shooter Oct 24 '15 at 15:56
  • @YasashiiEirian Instead of `.exe` of hello world.. cant you compile it to `dll` and use it? – Krsna Kishore Oct 24 '15 at 15:59
  • I think I have to keep HelloWorld.exe as is. – Second Person Shooter Oct 24 '15 at 16:01
  • @YasashiiEirian for that You need to make a class library and not a Console Application. The console application is translated into an .exe whereas the class library will then be compiled into a dll which you can reference in your windows project. `Right click on your Console Application (here hello world)-> Properties -> Change the Output type to Class Library` – Krsna Kishore Oct 24 '15 at 16:03
  • I will not make it as a class library because it will not reflect my objective. – Second Person Shooter Oct 24 '15 at 16:04
  • then last approach in my bag is [Inter-process communication](https://en.wikipedia.org/wiki/Inter-process_communication) – Krsna Kishore Oct 24 '15 at 16:12
  • So public Main in HelloWord.exe cannot be called from outside? Then what is the purpose of public in Main explained in MSDN? – Second Person Shooter Oct 24 '15 at 16:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93253/discussion-between-webruster-and-yasashii-eirian). – Krsna Kishore Oct 24 '15 at 16:29
  • @YasashiiEirian If you WANT, if you are marking it as public, means ex: you are turning a console app into an API.and The entry point of a program is marked with the`.entrypoint` IL directive. It does not matter if the method or the class is public or not, all that matters is this directive. – Krsna Kishore Oct 24 '15 at 16:34
  • @YasashiiEirian this is what i conclude 1)**Public**-If we want to initiate entry point by any external program,then you might need to make it public so it is accessible. 2)**Private**-If we know there is no external usage for the application then it is better to make it private so no external application access it. (Here when you make public using of Process can be able to acess it) – Krsna Kishore Oct 24 '15 at 16:36
  • `Process` can access regardless of either `public` or `private`, is it right? – Second Person Shooter Oct 24 '15 at 23:45
  • Yes you can start any process with `.exe` using process – Krsna Kishore Oct 26 '15 at 09:13