Click here to Skip to main content
15,887,328 members
Articles / Programming Languages / C# 8.0
Tip/Trick

Visual Studio Project Templates - How to Get Parameter Substitutions to Work Inside Strings

Rate me:
Please Sign up or sign in to vote.
4.98/5 (10 votes)
16 Jul 2023CPOL2 min read 10.4K   8   7
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.
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:

C#
[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:

C#
[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:

C#
[assembly: Guid("$guid1$")]

or:

C#
[assembly: Guid(8d23fc83-b21e-4d00-ab7a-9d7511987ae9)]

How to Fix It

Simple: add spaces ...

C#
[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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
QuestionThis should work Pin
pkfox26-Jul-23 22:40
professionalpkfox26-Jul-23 22:40 
Praisevery useful Pin
Southmountain20-Jul-23 20:19
Southmountain20-Jul-23 20:19 
Questionhoping there will be a part two demonstrating the ... Pin
BillWoodruff19-Jul-23 21:43
professionalBillWoodruff19-Jul-23 21:43 
AnswerRe: hoping there will be a part two demonstrating the ... Pin
OriginalGriff19-Jul-23 23:29
mveOriginalGriff19-Jul-23 23:29 
GeneralMy vote of 5 Pin
BillWoodruff19-Jul-23 21:40
professionalBillWoodruff19-Jul-23 21:40 
GeneralRe: My vote of 5 Pin
OriginalGriff19-Jul-23 23:09
mveOriginalGriff19-Jul-23 23:09 
GeneralMy vote of 3 Pin
Hill Phatpanichot17-Jul-23 2:48
Hill Phatpanichot17-Jul-23 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.