Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Main in a class, outside or under a namespace #31

Open
atifaziz opened this issue Sep 20, 2019 · 0 comments
Open

Support Main in a class, outside or under a namespace #31

atifaziz opened this issue Sep 20, 2019 · 0 comments

Comments

@atifaziz
Copy link
Owner

atifaziz commented Sep 20, 2019

It seems that #29 has more ramifications with respect to entry-point Main.

If Main is in a class in a namespace:

public static void Print(MethodBase method) =>
    Console.WriteLine($"{method.DeclaringType.FullName}::{method.Name}");

namespace MyApp1
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}

It gets called:

MyApp1.Program::Main

If there are two Main:

public static void Print(MethodBase method) =>
    Console.WriteLine($"{method.DeclaringType.FullName}::{method.Name}");

namespace MyApp1
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}
namespace MyApp2
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}

then the latter of the two is called:

MyApp2.Program::Main

If there is also a Main at root, however:

public static void Print(MethodBase method) =>
    Console.WriteLine($"{method.DeclaringType.FullName}::{method.Name}");

public static void Main(string[] args) =>
    Print(MethodBase.GetCurrentMethod());

namespace MyApp1
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}
namespace MyApp2
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}

then it is called instead:

UserQuery::Main

If there is a Main in a class without a namespace:

public static void Print(MethodBase method) =>
    Console.WriteLine($"{method.DeclaringType.FullName}::{method.Name}");

static class Program
{
    static void Main(string[] args) =>
        Print(MethodBase.GetCurrentMethod());
}

namespace MyApp1
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}
namespace MyApp2
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}

then it is called and trumps the other two:

UserQuery+Program::Main

Finally, a Main outside a class and a namespace:

public static void Print(MethodBase method) =>
    Console.WriteLine($"{method.DeclaringType.FullName}::{method.Name}");

public static void Main(string[] args) =>
    Print(MethodBase.GetCurrentMethod());

static class Program
{
    static void Main(string[] args) =>
        Print(MethodBase.GetCurrentMethod());
}

namespace MyApp1
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}
namespace MyApp2
{
    static class Program
    {
        static void Main(string[] args) =>
            Print(MethodBase.GetCurrentMethod());
    }
}

then it trumps all:

UserQuery+Program::Main

/cc @albahari

@atifaziz atifaziz changed the title Add support for Main in a class under a namespace Support Main in a class, outside or under a namespace Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant