-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (73 loc) · 2.38 KB
/
Program.cs
File metadata and controls
91 lines (73 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Tutorials
{
class Program
{
static void Main(string[] args)
{
while (true)
{
List<Tuple<string, Type>> tutorialTypes = new List<Tuple<string, Type>>();
foreach (Type t in Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.GetCustomAttributes(typeof(TutorialAttribute)).Any())
.OrderBy(t => t.Name))
{
tutorialTypes.Add(new Tuple<string, Type>(t.Name, t));
}
Type chosenType = DoMenu("Choose chapter", tutorialTypes);
if (chosenType == null)
return;
List<Tuple<string, MethodInfo>> tutorialMethods = new List<Tuple<string, MethodInfo>>();
foreach (MethodInfo mi in chosenType.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public).Where(mi => mi.GetCustomAttributes(typeof(TutorialAttribute)).Any()))
{
tutorialMethods.Add(new Tuple<string, MethodInfo>(mi.Name, mi));
}
MethodInfo chosenMethod = DoMenu(string.Format("{0} - Choose tutorial", chosenType), tutorialMethods);
if (chosenMethod == null)
continue;
Console.Clear();
Banner(string.Format("{0} . {1}", chosenType.Name, chosenMethod.Name));
Object o = chosenMethod.Invoke(null, new object[0]);
if (chosenMethod.ReturnType != typeof(void))
Console.WriteLine(o);
Console.WriteLine();
Console.WriteLine("press any key...");
Console.ReadKey();
}
}
private static T DoMenu<T>(string title, List<Tuple<string, T>> choices)
{
while (true)
{
Console.Clear();
Banner(title);
for (int i = 0; i < choices.Count; i++)
{
Console.WriteLine("{0,2} - {1}", i + 1, choices[i].Item1);
}
Console.WriteLine("99 - CANCEL / EXIT");
Console.Write(" >");
string choice = Console.ReadLine();
int chosen;
if (int.TryParse(choice, out chosen))
{
if (chosen == 99) return default(T);
if (chosen >= 1 && chosen <= choices.Count) return choices[chosen-1].Item2;
Console.WriteLine("Invalid choice");
}
}
}
private static void Banner(string title)
{
Console.WriteLine("=====================================================================");
Console.WriteLine(" " + title.ToUpper());
Console.WriteLine("=====================================================================");
}
}
}