Following is the content of the auto-generated <ProjectName>.GlobalUsings.g.cs
file when a Console Application or a Class Library project is created -
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
How can I modify the template for this file to add a few more namespaces?
Note: I know additional namespaces can be added through the project file, but I want to modify the default template for the auto-generated file.
Following is the content of the auto-generated <ProjectName>.GlobalUsings.g.cs
file when a Console Application or a Class Library project is created -
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
How can I modify the template for this file to add a few more namespaces?
Note: I know additional namespaces can be added through the project file, but I want to modify the default template for the auto-generated file.
Share Improve this question asked Mar 13 at 20:42 atiyaratiyar 8,3277 gold badges40 silver badges82 bronze badges 2 |1 Answer
Reset to default 3Per Selvin's comment what you probably DON'T want to modify is:
dotnet\sdk\VERSION\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.CSharp.props
which serves as the base source for the auto-generated usings file:
<!-- Implicit imports -->
<ItemGroup Condition="'$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable'">
<Using Include="System" />
<Using Include="System.Collections.Generic" />
<Using Include="System.IO" />
<Using Include="System.Linq" />
<Using Include="System.Net.Http" Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'"/>
<Using Include="System.Threading" />
<Using Include="System.Threading.Tasks" />
</ItemGroup>
However, your code will have issues when you
- Upgrade the SDK on your machine
- Try to build it on another machine where you haven't manually modified the SDK files
The recommended alternative is a "specifically named" GlobalUsings.cs
file that you just copy-paste to new projects where you put those extra usings:
global using System.Diagnostics;
.props
file(in sdk folder) and modify it but this doesn't make sense... You can always useDirectory.Build.props
to add some props to every project in solution – Selvin Commented Mar 13 at 21:01