New and improved JonSkeet.DemoUtil
It's amazing how sometimes small changes can make you very happy.
This week I was looking at how DragonFruit does its entry point magic, and realized I had a great use case for the same kind of thing.
Some of my oldest code that's still in regular use is ApplicationChooser - a simple tool for demos at conferences and user groups. Basically it allows you to write multiple classes with Main methods in a single project, and when you run it, it allows you to choose which one you actually want to run.
Until today, as well as installing the NuGet package, you had to create a Program.cs that calling ApplicationChooser.Run directly, and then explicitly set that as the entry point. But no more! That can all be done via build-time targets, so now it's very simple, and there's no extraneous code to explain away. Here's a quick walkthrough for anyone who would like to adopt it for their own demos.
Create a console projectIt's just a vanilla console app...
$ mkdir FunkyDemo$ cd FunkyDemo$ dotnet new consoleAdd the package, and remove the previous Program.cs
You don't have to remove Program.cs, but you probably should. Or you could use that as an entry point if you really want.
$ dotnet add package JonSkeet.DemoUtil$ rm Program.csAdd demo code
For example, add two files, Demo1.cs and Demo2.cs
// Demo1.csusing System;namespace FunkyDemo{ class Demo1 { static void Main() => Console.WriteLine("Simple example without description"); }}// Demo2.csusing System;using System.ComponentModel;using System.Threading.Tasks;namespace FunkyDemo{ // Optional description to display in the menu [Description("Second demo")] class Demo2 { // Async entry points are supported, // as well as the original command line args static async Task Main(string[] args) { foreach (var arg in args) { Console.WriteLine(arg); await Task.Delay(500); } } }}Run!
$ dotnet run -- abc def ghi0: Demo11: [Second demo] Demo2Entry point to run (or hit return to quit)?Conclusion
That's all there is to it - it's simple in scope, implementation and usage. Nothing earth-shattering, for sure - but if you give lots of demos with console applications, as I do, it makes life a lot simpler than having huge numbers of separate projects.