Roslyn csharpcompilation与全球隐式使用

发布于 2025-02-13 18:03:08 字数 217 浏览 2 评论 0原文

存在一种告诉csharpcompilation的方法。创建使用隐式使用吗?

Exists a way to tell CSharpCompilation.Create to use implicit usings?

Net 6 project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2025-02-20 18:03:08

编译器对隐式一无所知,这是SDK功能。正如@Guru Stron在评论中提到的那样,您可以在您创建的汇编中添加带有全局使用的语法树。全球用途的清单确实可能会有所不同。

全局使用树应该理想地以形式:

// <auto-generated/>
global using global::Namespace1;
global using global::Namespace2;
global using global::Namespace3;

主要集合:

// <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;

此列表来自 https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea /src/tasks/microsoft.net.build.tasks/targets/microsoft.net.sdk.csharp.props#l25-l25-l34

参见
https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea/src/src/websdk/websdk/websdk/web/web/sdk/sdk/sdk/sdk/sdk/sdk.props,propp,l64-l64-l74-l64-l74 /
https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea/src/src/src/blazorwasmsmsdk/sdk/sdk/sdk/sdk.propsupp-l23-l23-l23-l23-l23-l23-l23-l23-l23-l23-l23-l23-l23-p27 https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea/src/WebSdk/Worker/Targets/Microsoft.NET.Sdk.Worker.props#L41-L46

The compiler doesn't know anything about ImplicitUsings, it's an SDK feature. As @Guru Stron mentioned in a comment, you can add a syntax tree with the global usings to the compilation you're creating. The list of global usings can indeed vary.

The global usings tree should ideally be in the form:

// <auto-generated/>
global using global::Namespace1;
global using global::Namespace2;
global using global::Namespace3;

The main set are:

// <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;

This list comes from https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CSharp.props#L25-L34

See also
https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea/src/WebSdk/Web/Sdk/Sdk.props#L64-L74
https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea/src/BlazorWasmSdk/Sdk/Sdk.props#L23-L27
https://github.com/dotnet/sdk/blob/e797c37a264bf5b3a478baff0a085309228e0dea/src/WebSdk/Worker/Targets/Microsoft.NET.Sdk.Worker.props#L41-L46

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文