簡體   English   中英

如何使用更新的框架引用或復制其他 C# 項目的 output?

[英]How to reference or copy output of other C# project with a newer framework?

設想:

Loader.csproj,即.NET 4.8; 並輸出帶有 some.dll 依賴項的 a.exe
LoaderWrapper.csproj,即.NET 4.6,需要調用Loader的.exe和一些arguments。

Main.csproj,即.NET 4.6,正常引用LoaderWrapper。

我無法從 LoaderWrapper.csproj 創建對 Loader.csproj 的正常項目引用,因為 Loader.csproj 是較新的 .NET 框架。

我無法創建程序集引用,因為它只復制.exe 文件,而不是任何.dll 文件 Loader.csproj 的.exe 所依賴的文件。

我無法更改框架版本,因為我的應用程序在 3d 派對應用程序 Revit 中加載,即 .NET 4.6

我無法對 Loader.csproj 的 output 進行硬編碼,因為在我的構建管道中,我有幾個不同的 output 目錄。 (例如,用於單元測試)

在 Visual Studio 中,我可以更改 LoaderWrapper.csproj 上的“構建依賴項”->“項目依賴項”以確保構建 Loader.csproj,但這似乎並未復制/引用 Loader.csproj 的 output。

所以我正在尋找另一種方法來確保 Loader.csproj 的所有 output 都包含在 LoaderWrapper.csproj 中,並且也會通過項目引用最終出現在 Main.csproj 中。

前段時間我解決了類似的任務 - 從一個項目中收集內容文件並將它們包含到另一個項目的 output 中。 這與您的任務不完全相同,但您可以嘗試采用我的方法。

所以,我有一個名為 MainApp 的項目和一個名為 Lib 的項目。 Lib 項目具有 txt 文件 ContentFile.txt,其中 Build Action = Content。 我需要將 ContentFile.txt 包含到 MainApp 的 output 中。

我在 Lib 文件夾中創建了CustomBuildActions.targets文件,其內容如下

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!--
    Define project to get content files from.
    Definition relies on the fact that this target file stored
    in the same folder with Lib.csproj
    -->
    <ItemGroup>
        <LibProject Include="$(MSBuildThisFileDirectory)/Lib.csproj"/>
    </ItemGroup>

    <!--
    Run msbuild for Lib to collect the list of Content files and store it to the LibContentFiles list.
    Then perform string repace to convert paths to Content files to paths inside app bundle. And store results in the LibContentFileTargetPath.
    -->
    <Target Name="GetBundleFiles" Outputs="@(LibContentFiles)">
        <MSBuild Projects="@(LibProject)" Targets="ContentFilesProjectOutputGroup">
            <Output ItemName="LibContentFiles" TaskParameter="TargetOutputs"/>
        </MSBuild>

        <ItemGroup>
            <LibContentFileTargetPath Include="@(LibContentFiles->Replace($(MSBuildThisFileDirectory), $(AppBundleDir)/Contents/Resources/))"/>
        </ItemGroup> 
    </Target>

    <!-- These targets will fire after mmp creates your bundle but before code signing -->
    <PropertyGroup>
        <CreateAppBundleDependsOn>$(CreateAppBundleDependsOn);GetBundleFiles;CopyOurFiles;</CreateAppBundleDependsOn>
    </PropertyGroup>

    <!-- Since this has inputs/outputs, it will fire only when the inputs are changed or the output does not exist -->
    <Target Name="CopyOurFiles" Inputs="@(LibContentFiles)" Outputs="@(LibContentFileTargetPath)">
        <Message Text="This is us copying a file into resources!" />
        <!-- This could have easily been done w/ a built in build action, but you can extend it arbitrary. -->
        <Copy SourceFiles="@(LibContentFiles)" DestinationFiles="@(LibContentFileTargetPath)" />
</Target>
</Project>

然后在MainApp項目中導入這個target

  <Import Project="../Lib/CustomBuildActions.targets" />

此自定義目標將從 Lib 項目收集內容文件,轉換路徑以保留 MainApp output 中的文件夾結構(例如,如果我們將在 Lib 項目內部有類似 Lib/ContentFolder/Subfolder/contentFile.txt 的內容,那么如果將在包中放置在 Resources/ContentFolder/Subfolder/contentFile.txt) 中,然后復制包內的文件。

因此,您需要采取的措施:

  1. 使用 Loader 項目的 output 項目而不是內容文件。 因此,您將需要其他目標,而不是ContentFilesProjectOutputGroup 請參閱 msbuild 文檔以找到最適合您的。
  2. 更改目標依賴項 - 您的自定義目標應該成為 LoaderWrapper 項目構建步驟的依賴項(在我的示例中,我使用CreateAppBundleDependsOn依賴項,但它僅適用於 Xamarin,您需要找到更好的步驟來添加依賴項)。

我的案例的完整描述可以在這里找到https://forums.xamarin.com/discussion/comment/418130

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM