-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProgram.cs
70 lines (59 loc) · 2.27 KB
/
Program.cs
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
// Licensed under the Apache License, Version 2.0 (the "License").
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading.Tasks;
[assembly: NeutralResourcesLanguage("en-GB")]
[assembly:
SuppressMessage("Usage", "CA2254:Template should be a static expression", Justification = "This is sample code")]
namespace HIDDevices.Sample;
internal static class Program
{
private static async Task Main(string[] args)
{
var assembly = Assembly.GetExecutingAssembly();
var samples = assembly
.GetTypes()
.Where(t => !t.IsAbstract &&
(t.IsValueType ||
(t.GetInterfaces().Contains(typeof(ISample)) &&
t.GetConstructor(Type.EmptyTypes) != null)))
.Select(Activator.CreateInstance)
.OfType<ISample>()
.ToArray();
ISample? sample;
if (args.Length != 1 ||
(sample = Array.Find(samples, s => s.ShortNames.Contains(args[0]))) is null)
{
var assemblyName = assembly.GetName().Name;
// We appear to have a cry for help!
Console.WriteLine(Resources.SampleExecutor, assemblyName);
Console.WriteLine();
do
{
Console.WriteLine(Resources.SelectSample);
// Create instances of all sample classes
foreach (var s in samples)
{
Console.WriteLine(
Resources.SampleDescription, Environment.NewLine,
string.Join('|', s.ShortNames),
s.FullName,
s.Description);
}
if (!Environment.UserInteractive)
{
return;
}
var option = Console.ReadLine();
sample = Array.Find(samples, s => s.ShortNames.Contains(option));
} while (sample is null);
}
Console.WriteLine(Resources.RunningSample, sample.FullName);
Console.WriteLine();
await sample.ExecuteAsync().ConfigureAwait(false);
}
}