我的源代码生成器依赖于ProjectReference
和Basic.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>
1条答案
按热度按时间bzzcjhmw1#
如何让它工作
1.将
GeneratePathProperty="true" PrivateAssets="all"
添加到从属nuget包中。1.修改
GetTargetPathDependsOn
以附加其他目标。1.创建目标
GetDependencyTargetPaths
,并将依赖项打包到nuget的analyzers/dotnet/cs
中。1.如果依赖项仅用于生成器,请使用
<IncludeBuildOutput>false</IncludeBuildOutput>
停用nuget内的库的生成以下是完整示例