无法将本机 .dll 引用添加到源生成器项目

Bli*_*ndy 8 c# code-generation roslyn-code-analysis

您可以在https://github.com/myblindy/GrimBuilding/tree/efcore(分支)找到完整的源代码efcore

我知道源生成器无法自动从 nuget 包中获取依赖项,并且您必须使用笨重的解决方法才能使其正常工作,而我已经这样做了。这是我的源生成器项目:

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.0-1.final" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
    <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.*-*" GeneratePathProperty="true" PrivateAssets="all" />
    <PackageReference Include="Microsoft.Data.Sqlite.Core" Version="6.*-*" GeneratePathProperty="true" PrivateAssets="all" />
    <PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.*-*" GeneratePathProperty="true" PrivateAssets="all" />
    <PackageReference Include="SQLitePCLRaw.provider.e_sqlite3" Version="2.*-*" GeneratePathProperty="true" PrivateAssets="all" />
  </ItemGroup>

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

  <Target Name="GetDependencyTargetPaths">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(PKGSQLitePCLRaw_bundle_e_sqlite3)\lib\netstandard2.0\SQLitePCLRaw.batteries_v2.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PKGSQLitePCLRaw_provider_e_sqlite3)\lib\netstandard2.0\SQLitePCLRaw.provider.e_sqlite3.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PKGSQLitePCLRaw_lib_e_sqlite3)\runtimes\win-x64\native\e_sqlite3.dll" IncludeRuntimeDependency="false" />
      <TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Data_Sqlite_Core)\lib\netstandard2.0\Microsoft.Data.Sqlite.dll" IncludeRuntimeDependency="false" />
    </ItemGroup>
  </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)

由于也没有任何传递支持,因此我Microsoft.Data.Sqlite一一添加了每个嵌套包,生成了它们的路径属性并使用TargetPathWithTargetPlatformMoniker. 这一切都有效,直到我到达 native e_sqlite3.dll,如果我TargetPathWithTargetPlatformMoniker与它一起使用,它会尝试将其作为托管库引用,并且会按预期失败:

4>CSC : 警告 CS8034: 无法加载分析器程序集 C:\Users\meep.nuget\packages\sqlitepclraw.lib.e_sqlite3\2.0.5-pre20210521085756\runtimes\win-x64\native\e_sqlite3.dll : PE 映像不不包含托管元数据。

因此,鉴于已找到路径,是否可以使用不同的标签来使主项目复制文件,e_sqlite3.dll以便分析器可以使用它?

8vt*_*two 0

摘自.NET 项目 SDK 概述

构建事件

在 SDK 样式项目中,使用名为 PreBuild 或 PostBuild 的 MSBuild 目标,并设置 PreBuild 的 BeforeTargets 属性或 PostBuild 的 AfterTargets 属性。

 <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="&quot;$(ProjectDir)PreBuildEvent.bat&quot; &quot;$(ProjectDir)..\&quot; &quot;$(ProjectDir)&quot; &quot;$(TargetDir)&quot;" />
</Target>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
   <Exec Command="echo Output written to $(TargetDir)" />
</Target>
Run Code Online (Sandbox Code Playgroud)

也许您可以使用这些指令之一手动移动文件?

还有这个:

<Content Include="$(OutputPath)\*.dll;$(OutputPath)\*.json">
      <Pack>true</Pack>
      <PackagePath>build\</PackagePath>
</Content>
Run Code Online (Sandbox Code Playgroud)

你能像这样移动文件吗?