源发电机在Visual Studio 2022中无法正常工作

发布于 2025-02-13 18:58:51 字数 726 浏览 0 评论 0 原文

我正在编写一个源生成器,该生成器会自动编写JSONOBJECT类,该类将JSON转换为运行时对象。

如屏幕截图中所示,您可以看到红色波浪线。它们是 CS0246 。但是,该项目可以成功构建而没有任何错误。这意味着源生成器有效。

是否可以让编辑器识别源生成器的工作?

此外,在解决方案资源管理器中,它显示了“该生成器没有生成文件”。这显然是不正确的。

I am writing a source generator that automatically writes JsonObject classes, that translantes JSON to run-time objects.

enter image description here

As shown in the screenshot, you can see red wavy lines. These are CS0246. However, the project can be successfully built without any error. That means the source generator works.

Is it possible to let the editor recognize the work of source generator?

Furthermore, in the solution explorer, it shows "This generator is not generating files." This is obviously incorrect.

enter image description here

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

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

发布评论

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

评论(1

甜尕妞 2025-02-20 18:58:51

最终在Visual Studio 17.8.4中固定。它在17.8.3中没有工作。

这是我的生成器项目的 .csproj

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
        <PackageReference Include="Scriban" Version="5.9.1" GeneratePathProperty="true" />
    </ItemGroup>

    <PropertyGroup>
        <GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
    </PropertyGroup>

    <Target Name="GetDependencyTargetPaths">
        <ItemGroup>
            <TargetPathWithTargetPlatformMoniker Include="$(PKGScriban)\lib\netstandard2.0\Scriban.dll" IncludeRuntimeDependency="false" />
        </ItemGroup>
    </Target>

</Project>

与各种教程相反,我认为在这里使用 privateassets =“ all” 在此处使用。

我的消费者居住在同一解决方案中,并使用 ProjectReference

    <ItemGroup>
        <ProjectReference Include="..\TestGenerator\TestGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    </ItemGroup>

This was finally fixed in Visual Studio 17.8.4. It did not work in 17.8.3.

This is the .csproj for my generator project:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
        <PackageReference Include="Scriban" Version="5.9.1" GeneratePathProperty="true" />
    </ItemGroup>

    <PropertyGroup>
        <GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
    </PropertyGroup>

    <Target Name="GetDependencyTargetPaths">
        <ItemGroup>
            <TargetPathWithTargetPlatformMoniker Include="$(PKGScriban)\lib\netstandard2.0\Scriban.dll" IncludeRuntimeDependency="false" />
        </ItemGroup>
    </Target>

</Project>

Contrary to various tutorials, I did not find it necessary to use PrivateAssets="all" here.

My consumer resides in the same solution and references the generator with a ProjectReference:

    <ItemGroup>
        <ProjectReference Include="..\TestGenerator\TestGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    </ItemGroup>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文