.net C#源代码生成器:无法加载文件或程序集

u4vypkhs  于 2022-12-27  发布在  .NET
关注(0)|答案(1)|浏览(318)

我的源代码生成器依赖于ProjectReferenceBasic.Reference.Assemblies。当我调试它时,生成器工作,但当我编译一个使用生成器的项目时,它会说:

CSC : warning CS8785: Generator 'Generator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Basic.Reference.Assemblies, Version=1.0.0.0, Culture=neutral, PublicKeyToken=00aeae93c2ffe759' or one of its dependencies. The system cannot find the file specified.'

我很确定我需要把DLL和分析器放在一起,但是我不能让它工作。
我尝试在项目文件中设置依赖项,类似于roslyn-sdk的示例:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftNetCompilersToolsetVersion)" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
  </ItemGroup>

  <ItemGroup>
    <!-- Generator dependencies -->
    <PackageReference Include="CsvTextFieldParser" Version="1.2.2-preview" GeneratePathProperty="true" PrivateAssets="all" />
  </ItemGroup>

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

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

https://github.com/dotnet/roslyn-sdk/blob/main/samples/CSharp/SourceGenerators/SourceGeneratorSamples/CSharpSourceGeneratorSamples.csproj
相关问题:

bzzcjhmw

bzzcjhmw1#

如何让它工作

1.将GeneratePathProperty="true" PrivateAssets="all"添加到从属nuget包中。
1.修改GetTargetPathDependsOn以附加其他目标。

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

1.创建目标GetDependencyTargetPaths,并将依赖项打包到nuget的analyzers/dotnet/cs中。

<Target Name="GetDependencyTargetPaths">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(PKGCsvTextFieldParser)\lib\netstandard2.0\CsvTextFieldParser.dll" IncludeRuntimeDependency="false" />
      <!-- Pack both our DLL and the dependencies into a generated Nuget Package -->
      <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
      <None Include="$(PKGCsvTextFieldParser)\lib\netstandard2.0\CsvTextFieldParser.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    </ItemGroup>
  </Target>

1.如果依赖项仅用于生成器,请使用<IncludeBuildOutput>false</IncludeBuildOutput>停用nuget内的库的生成

以下是完整示例

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

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <LangVersion>latest</LangVersion>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <IsRoslynComponent>true</IsRoslynComponent>
        <PreserveCompilationContext>true</PreserveCompilationContext>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
        <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
        <IncludeBuildOutput>false</IncludeBuildOutput>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>

    <ItemGroup>        
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" PrivateAssets="all" />
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
        <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
        <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.4.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
        <!-- Generator dependencies -->
        <PackageReference Include="Core" Version="1.0.0" GeneratePathProperty="true" PrivateAssets="all" />
        <PackageReference Include="Basic.Reference.Assemblies" Version="1.4.1" GeneratePathProperty="true" PrivateAssets="all" />
    </ItemGroup>

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

    <Target Name="GetDependencyTargetPaths">
        <ItemGroup>
            <TargetPathWithTargetPlatformMoniker Include="$(PKGCore)\lib\netstandard2.0\Core.dll" IncludeRuntimeDependency="false" />
            <TargetPathWithTargetPlatformMoniker Include="$(PKGBasic_Reference_Assemblies)\lib\netstandard2.0\Basic.Reference.Assemblies.dll" IncludeRuntimeDependency="false" />
            <!-- Pack both our DLL and the dependencies into a generated Nuget Package -->
            <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
            <None Include="$(PKGCore)\lib\netstandard2.0\Core.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
            <None Include="$(PKGBasic_Reference_Assemblies)\lib\netstandard2.0\Basic.Reference.Assemblies.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
        </ItemGroup>
    </Target>
</Project>

相关问题