I am switching to C# 8 / .NET 7 so I'm recreating my project templates to create suitable versions. And it's not trivial as some things are very different. No Application Guid for starters - and that's important as my apps forms remember size and location via a folder using the App Guid as part of the path. So I needed to add one.
Introduction
Why am I switching? Because .NET 4.8 doesn't support Default Implementations for Interfaces even with a compiler that does - and I have an application where this would be a really tidy way to do it.
Switching to C# 8 / .NET 7 from C# 7 / .NET 4.8 should be simple, but it can be a PITA - the changes of some method return values to nullable reference (but not all of them) caused me some headaches in working out what the new errors and warnings actually mean and why I get them on one bit of code but not a near identical one: Assembly.GetEntryAssembly
now returns a nullable reference, but Assembly.GetExecutingAssembly
doesn't ...
But my big problem was the removal of the Application Guid which was preloaded by the IDE when you created a Winforms project. It's not in the new, shiny, .NET 7 version at all ...
Getting a Guid
It's fairly easy - you need to add a AssemblyInfo.cs file:
- Right click the project and select Properties.
- Select the Build tab.
- Scroll down to the option Generate Assembly info and tick it.
If you look in there, you'll find it there - it'll be similar to this:
[assembly: Guid("8d23fc83-b21e-4d00-ab7a-9d7511987ae9")]
Now all you have to do is replace the "fixed stuff" in your "templatable" project with template parameters: Template Parameters | Microsoft Learn[^]
The one you want is $guid1$
which needs to be part of a string
to compile. So the obvious thing to do is stick the parameter name in the string
:
[assembly: Guid("$guid1$")]
And when you build your new project from the resulting template, it won't compile or won't run ... because the parameter is in a string
, and it doesn't substitute it. What you get in the file is:
[assembly: Guid("$guid1$")]
or:
[assembly: Guid(8d23fc83-b21e-4d00-ab7a-9d7511987ae9)]
How to Fix It
Simple: add spaces ...
[assembly: Guid(" $guid1$ ")]
The template system will keep the double quotes, throw away the spaces, and do the substitution with a shiny new Guid. Don't ask me why it works - just accept that it does.
Points of Interest
I learned again that there are times when Microsoft should leave things well alone ... if it isn't broken, don't fix it!
History
- V1.0 16th July, 2023 First version