1. ホーム
  2. Web プログラミング
  3. ASP.NET

NET 6 の今後の新機能 暗黙の名前空間参照

2022-01-14 10:01:41

まえがき

を書かなくてもできるアップデートを今日見ました。 using まだ正式にはリリースされていません。 NET 6 Preview 7 がサポートされるのは

1. Sample
We start with 
Minimal API 
as an example
Example.
Using Microsoft.AspNetCore.Builder;

var app = WebApplication.Create(args);
app.Map("/", () => "Hello World");
app.Run();



Or combine it with 
global using 
followed by the 
using 
in a separate 
Imports 
file,
Imports:
@global using Microsoft.AspNetCore;




Program:
var app = WebApplication.Create(args);
app.Map("/", () => "Hello World");
app.Run();




NET 6 Preview 7, we will no longer need to add the 
using 
in the
Preview 7
 will include a new feature, "implicit namespace references", which will add common namespaces to your project by default, so we don't need the 
Imports 
above.
2. How it works
It is also implemented based on the previous
 global using 
feature, where the compiler will use the
 MS Build 
namespace configured in the 
global using 
file, just like the 
Imports
Currently different defaults are added for different SDK types 
namespace
which is currently available as follows.
For
 Sdk for Microsoft.
, the default namespace is as follows.
System
System.Collections.Generic
System.IO
System.Linq
System.Net.Http
System.Threading
System.Threading.Tasks




In response to
 Microsoft.NET.Sdk.Web
System.Net.Http.
Microsoft.AspNetCore.
Hosting
Microsoft.AspNetCore.Http
Microsoft.AspNetCore.
Microsoft.Extensions.Configuration
DependencyInjection
Microsoft.Extensions.Hosting
Microsoft.Extensions.Logging



Logging 
In response to 
Microsoft.NET.Sdk.Worker
Microsoft.Extensions.Configuration
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Hosting
Microsoft.Extensions.Logging




If you want to disable implicit namespace references, you can do so with the 
DisableImplicitNamespaceImports 
to disable this feature entirely
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>




If you just want to disable the ability to target 
NET.Sdk.Web 
then you can configure the
 DisableImplicitNamespaceImports_Web
<DisableImplicitNamespaceImports_Web>true</DisableImplicitNamespaceImports_Web>




Configuration correspondence.

There should be more support later, depending on the final .NET6 release
We can also enable
 global using 
so that we can also use it without using
You just need to package the project file with a new line like the following 
property 
to the project
<Project>
  <ItemGroup>
    <GlobalUsings Include="Library" />
  </ItemGroup>
</Project>



3. More
Implicit namespace references allow us to avoid writing particularly repetitive namespace references, which can be very convenient for simple test applications
While implicit namespace references can greatly simplify our code by eliminating the need to write repetitive 
using 
code, it can also cause conflicts in the code, such as when you define a class that exists under your own namespace and under the default implicitly referenced namespace, and if you can't remove the default namespace reference, then you need to use a fully qualified name or alias, which can be a little inconvenient
NET 6's upcoming new feature, implicit namespace references, is here, more related .

There should be more support later, depending on the final .NET6 release

We can also enable
 global using 
so that we can also use it without using

 global using 

You just need to package the project file with a new line like the following 
property 
to the project

property 

3. More

Implicit namespace references allow us to avoid writing particularly repetitive namespace references, which can be very convenient for simple test applications

<テーブル SDK プロパティ名 Microsoft.NET。 DisableImplicitNamespaceImports_DotNet Microsoft.NET.Sdk.Web DisableImplicitNamespaceImports_Web Microsoft.NET.Sdk.Worker DisableImplicitNamespaceImports_Worker。

using 

NET 6's upcoming new feature, implicit namespace references, is here, more related .