.NET 6 中的隐式命名空间引用 |
|
前言: 之前写过一篇隐式命名空间引用的大概介绍,在一些小的测试项目中也有在用,一直没作为示例给大家分享,主要原因在于之前看到了一个关于隐式命名空间引用的 1、历史变化
在 Preview 7 中如果想要使用新加一个 namespace 引用,可以在项目文件中配置 <Import 类似地,如果要移除某一个命名空间的引用,之前是 2、新版本介绍使用
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
相比 .NET 6 Preview 7 多了一个 3、More usage来看下面的这个示例:
MyFile.Exists("");
WriteLine("Hello, World!");
InvokeHelper.TryInvoke(() => WriteLine("ImplicitUsingSample"));
项目文件中自定义的命名空间引用
<ItemGroup>
<Using Include="System.Console" Static="true" /> <!-- Global using static -->
<Using Include="WeihanLi.Common.Helpers" /> <!-- Global using -->
<Using Include="System.IO.File" Alias="MyFile" /> <!-- Global using alias -->
<Using Remove="System" /> <!-- Remove namespace using -->
</ItemGroup>
自动生成的 // <auto-generated/> 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; global using global::WeihanLi.Common.Helpers; global using MyFile = global::System.IO.File; global using static global::System.Console;
默认的命名空间引用和所使用的 SDK 有关系,目前确定的如下:
4、More个人觉得这是一个好的变更,如果默认启用的话,有很多项目在升级的时候可能就会有冲突导致无法正常的编译,而默认禁用,升级的时候就能避免这种 有了 使用起来可能会发现有一些 BUG ==,发现 BUG 在 如果没有体验过 .NET 6 的 Preview 版本,非常推荐尝试一下 .NET 6 RC 1,新功能已经基本确定了,进行更多的测试来努力让 .NET 6 成为一个高质量的 到此这篇关于.NET 6 中的隐式命名空间引用的文章就介绍到这了,更多相关.NET 6 中的隐式命名空间引用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! |