Update off Kevin's code review
MODIFY
src/Shared/Program.cs
src/Shared/Program.cs
@@ -16,12 +16,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
IEnumerable<string> stringArguments = new string[0];
var assembly = Assembly.GetEntryAssembly();
- string regexMatch = Regex.Match(assembly.FullName.Split(',')[0], "\\d{1,2}").Value;
+ string regexMatch = Regex.Match(assembly.GetName().Name, "\\d{1,2}").Value;
- int.TryParse(regexMatch, out int chapterNumber);
+ int chapterNumber = int.Parse(regexMatch);
if (args.Length == 0)
{
- Console.Write($"Enter the listing number to execute (e.g. For Listing {chapterNumber}.1 enter \"{chapterNumber}.1\"): ");
+ Console.Write(
+ $"Enter the listing number to execute (e.g. For Listing {chapterNumber}.1 enter \"{chapterNumber}.1\"): ");
input = Console.ReadLine();
}
else
@@ -44,12 +45,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
{
throw new InvalidOperationException($"There is no listing '{input}'.");
}
+
MethodInfo method = target.GetMethods().First();
string[]? arguments;
if (!method.GetParameters().Any())
{
- arguments = null; // If there are no parameters to the method, the arguments parameter should be null.
+ arguments =
+ null; // If there are no parameters to the method, the arguments parameter should be null.
}
else
{
@@ -62,20 +65,28 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
arguments = stringArguments.ToArray();
}
}
+
if (method.GetCustomAttributes(typeof(STAThreadAttribute), false).Any())
{
- Thread thread = new Thread(() => method.Invoke(null, arguments));
- //thread.SetApartmentState(ApartmentState.STA);
- thread.Start();
- thread.Join();
+ Thread thread = new Thread(() =>
+ {
+ object result = method.Invoke(null, arguments);
+ if (!(method.ReturnType == typeof(void)))
+ {
+ Console.WriteLine($"Result: {result}");
+ }
+ });
}
else
{
var result = method.Invoke(null, arguments);
+ if (!(method.ReturnType == typeof(void)))
+ {
Console.WriteLine($"Result: {result}");
}
}
+ }
catch (System.IO.FileNotFoundException)
{
Console.ForegroundColor = ConsoleColor.Red;
@@ -118,7 +129,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
else
{
Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(string.Format("Listing {0} threw an exception of type {1}.", input, exception.GetType()));
+ Console.WriteLine(string.Format("Listing {0} threw an exception of type {1}.", input,
+ exception.GetType()));
}
}
finally
@@ -157,6 +169,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
{
args = userArguments.Split(new[] {' '});
}
+
return args;
}
@@ -175,7 +188,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
{
startPosition = 1;
listing += chapterListing[0].ToUpper() + ".";
- chapterName = "Chapter" + (appendices.Contains(chapterListing[0].ToUpper()) ? "App" : "") + chapterListing[0];
+ chapterName = "Chapter" + (appendices.Contains(chapterListing[0].ToUpper()) ? "App" : "") +
+ chapterListing[0];
}
else
{
@@ -184,26 +198,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
for (int index = startPosition; index < chapterListing.Length; index++)
{
- if (index == startPosition && string.IsNullOrEmpty(chapterName)) chapterName = "Chapter" + chapterListing[index].PadLeft(2, '0');
+ if (index == startPosition && string.IsNullOrEmpty(chapterName))
+ chapterName = "Chapter" + chapterListing[index].PadLeft(2, '0');
listing += chapterListing[index].PadLeft(2, '0')
+ ((index + 1 != chapterListing.Length) ? "." : "");
}
- if (listing.Split('.').Length > 2) // 02.01.02.06
- {
- List<int> indexes = new List<int>();
-
- for (var i = 0; i < listing.Length; i++)
- {
- if (listing[i] == '.')
+ string[] parts = listing.Split('.'); // 02.01.02.06
+ if (parts.Length > 2)
{
- indexes.Add(i);
- }
- }
-
- string toReplace = listing.Substring(indexes[1], indexes[2]-indexes[1]+1);
-
- listing = listing.Replace(toReplace, "To");
+ listing = $"{parts[0]}.{parts[1]}To{string.Join('.', parts.Skip(3))}";
}
return listing.Replace('.', '_');

Update off Kevin's code review