Created
December 15, 2020 03:48
-
-
Save bryanjhv/e60dc6df0a05e4d35b92b7c01a840ac6 to your computer and use it in GitHub Desktop.
C# program menu (searches classes and runs Main method)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Reflection; | |
namespace ConsoleApp1 { | |
class Program { | |
static void Main(string[] args) { | |
var current = MethodBase.GetCurrentMethod().DeclaringType; | |
var methods = ( | |
from type in Assembly.GetExecutingAssembly().GetTypes() | |
where type.Namespace == current.Namespace | |
where type.IsClass | |
where type.Name.EndsWith("Program") | |
where type != current | |
select type.GetMethod("Main", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, CallingConventions.Standard, new Type[] { typeof(string[]) }, null) into method | |
where method != null | |
select method | |
).ToArray(); | |
int index = 0; | |
while (true) { | |
if (index == -1) | |
Console.WriteLine("Wrong option selected."); | |
if (index == -2) | |
Console.WriteLine("Wrong option inputted."); | |
if (index < 0) | |
Console.WriteLine(); | |
Console.WriteLine("PROGRAMS LIST:"); | |
int i = 1; | |
foreach (var method in methods) | |
Console.WriteLine($"{i++}. {method.DeclaringType.Name}"); | |
Console.Write("OPTION: "); | |
if (!int.TryParse(Console.ReadLine(), out index)) | |
index = -2; | |
else if (index < 1 || index > methods.Length) | |
index = -1; | |
else { | |
index--; | |
Console.WriteLine(); | |
break; | |
} | |
} | |
methods[index].Invoke(null, new object[] { args }); | |
Console.WriteLine(); | |
Console.WriteLine("Press any key to exit..."); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment