我正在源代码生成器上工作,我有依赖关系的问题:
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 'Flurl.Http, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.'
有很多关于如何将依赖项打包到nuget中的信息,但我直接引用Analyzer项目,如下所示:
<ProjectReference Include="SG.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
在分析器项目中,我添加了<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
,所有依赖项都在输出目录中可用,但VS没有使用该目录-它使用AppData\Local\Temp\VBCSCompiler\AnalyzerAssemblyLoader\[...]
,并且只在那里复制了一个DLL。
我们可以做些什么来实现这一目标?
4条答案
按热度按时间3lxsmp7m1#
现在对此有了明确的答案。Source Generator Samples包含一个示例
.csproj
,它具有所需的设置。我用这个做了一个Sprache的源代码生成器,效果很好。
将示例
.csproj
粘贴到此处以供参考:goucqfw62#
ProjectReference
与PackageReference
不相同!GeneratePathProperty不会对
ProjectReference
执行任何操作,因为它不是包。因此,您只需将指向本地DLL的
TargetPathWithTargetPlatformMoniker
添加到源代码生成器csproj
中。默认输出位置示例:
文件夹结构
我的源生成器.csproj
anauzrmj3#
我找到了方法,使它的工作或多或少的可靠与一些黑客。
在此之前,我也尝试过ILMerge,但它不起作用(缺少方法异常)。
溶液:
首先,我在源代码生成器汇编中嵌入了依赖项,如下所示:
然后我为
AppDomain
(生成器类中的静态构造函数)创建了AssemblyResolve
处理程序,如下所示:pcrecxhr4#
源代码生成器食谱中概述了这种 * 应该 * 工作的方式,其示例如下:
但是,根据我的经验,这仍然不可靠或不健壮;这是公认的,我得到的印象是,改善这种经验是未来规划的一部分,但:现在,老实说,最好不要使用核心框架和已经加载的Roslyn库之外的依赖项。2如果你的依赖项和分析器/生成器在同一个repo中,你可以在构建期间简单地吸入代码,例如从这里: