Directory.Build.props: C# 9 for all your projects
source link: https://blog.johnnyreilly.com/2021/07/14/directory-build-props-c-sharp-9-for-all/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Directory.Build.props: C# 9 for all your projects
.NET Core can make use of C# 9 by making some changes to your .csproj
files. There is a way to opt all projects in a solution into this behaviour in a single place, through using a Directory.Build.props
file and / or a Directory.Build.targets
file. Here's how to do it.
"have you the good news about Directory.Build.props
"?#
I wrote recently about using C# 9 with in-process Azure Functions. What that amounted to, was using C# 9 with .NET Core.
One of the best things about blogging, is all that you get to learn along the way. After I put up that post, Daniel Earwicker was kind enough to send this message:
I was intrigued that Daniel was able to configure all the projects in a solution to use the same approach using some strange incantations named Directory.Build.props
and Directory.Build.targets
. Microsoft describes them thusly:
Prior to MSBuild version 15, if you wanted to provide a new, custom property to projects in your solution, you had to manually add a reference to that property to every project file in the solution. Or, you had to define the property in a
.props
file and then explicitly import the.props
file in every project in the solution, among other things.However, now you can add a new property to every project in one step by defining it in a single file called
Directory.Build.props
in the root folder that contains your source.
Let's see if we can put it to use.
Directory.Build.props
: C# 9 for all#
So, rather than us updating each of our .csproj
files, we should be able to create a Directory.Build.props
file to sit alongside our .sln
file in the root of our source code. We'll add this into the file:
<Project> <PropertyGroup> <!-- use C# 9 --> <LangVersion>9.0</LangVersion> </PropertyGroup> <ItemGroup> <!-- allows some C# 9 support with .NET Core 3.1 https://github.com/manuelroemer/IsExternalInit --> <PackageReference Include="IsExternalInit" Version="1.0.1"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> </ItemGroup></Project>
Now we're free to add projects into the solution, which will already support C# 9 without us taking any further steps. It's as simple as that! Thanks to Daniel for sharing this super handy tip. ❤️🌻
Recommend
-
8
There’s a common pattern among free- and open-source software projects to include a “contrib” directory at the top of their source code tree. I’ve seen this in many projects for many years, but I’ve seen it discussed only...
-
12
List all files (recursively) in a directory yourbasic.org/golang Directory listing Use the ioutil.ReadDir function in package
-
12
Add a "contrib" directory to your projects June 6, 2020 on Drew DeVault's blog There’s a common pattern among free- and open-source software projects to include a...
-
13
My 737 MAX Simulator RideSouthwest 737 MAX at Boeing Field. Photo: John Crowley from Seattle, WA, USA, CC BY-SA 2.0 , via Wikimedia CommonsIt has been nearly two years since the 737 MAX was grounded in the aftermath of several crashes. After...
-
3
Instantiation of all classes in the directory advertisements I'm using Laravel and creating artisan commands but I need to...
-
5
[Bash] List All Files in Directory Recursively and Rename February 02, 2015
-
1
Siv Scripts Solving Problems Using Code Yesterday I gave an abridged version of my
-
6
Configure Netlify redirects for projects with custom publish or base directoryPublished: 2023.02.25 · 2 minutes readNetlify supports two method...
-
2
Sergio Guerrero April 25, 2023 6 minute read...
-
2
Understand Directory.Build.props: Centralizing .NET Project Configurations March 27, 2024 4 minutes read
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK