最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How do I modify the template for the auto-generated global using file? - Stack Overflow

programmeradmin2浏览0评论

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
  • You prolly could find right .props file(in sdk folder) and modify it but this doesn't make sense... You can always use Directory.Build.props to add some props to every project in solution – Selvin Commented Mar 13 at 21:01
  • @Selvin, I just don't to make it a repeated task whenever I'm creating a new project. – atiyar Commented Mar 13 at 21:12
Add a comment  | 

1 Answer 1

Reset to default 3

Per 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

  1. Upgrade the SDK on your machine
  2. 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;
发布评论

评论列表(0)

  1. 暂无评论