简体   繁体   中英

Visual Studio Solution(.sln) / C#: Start a project with another project's dll as parameter

is it possible to run a project with an argument of a dll from another project?

My project structure and dependencies look like this

在此处输入图像描述 在此处输入图像描述

As you can see there are no direct dependencies between a mod and the game implementation directly.

When I click on Debug -> Start New Instance , F5 or use the button ( 在此处输入图像描述 ) I want to run ./ExampleGameInEngineA.exe GameModC.dll .

I know that you can set an Executable in the project settings 在此处输入图像描述

But I am looking for a more generic way that is automatically compiling ExampleGameInEngineA and putting it in the same directory as GameModC . Hardcoded directory paths are also not great (tho I could use relative paths for this).

For testing purposes we can safely ignore GameInEngineB but I still don't want any hard references to GameInEngineA .

Okay, I kinda solved it by myself by using a PreBuild-Event-Target:

GameModC.csproj

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

    <PropertyGroup>
        <TargetFramework>netstandard2.1</TargetFramework>
        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
        <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
    </PropertyGroup>

    <Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="">
        <MSBuild Projects="$(SolutionDir)ExampleGameInEngineA\ExampleGameInEngineA.csproj..." />
        <ItemGroup>
            <GameEngineAOutputFolder Include="$(SolutionDir)ExampleGameInEngineA\$(OutDir)*.*" />
        </ItemGroup>
        <Copy SourceFiles="@(GameEngineAOutputFolder)" DestinationFolder="$(OutDir)" SkipUnchangedFiles="false" />
    </Target>

    <ItemGroup>
      <ProjectReference Include="..\GameApi\GameModdingApi.csproj" />
    </ItemGroup>

</Project>

GameInEngineA.csproj

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

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    </PropertyGroup>

    <ItemGroup>
        <ProjectReference Include="..\GameApi\GameModdingApi.csproj" />
    </ItemGroup>

</Project>

Please note the AppendTargetFrameworkToOutputPath to remove the netcoreapp3.1 or netstandard2.1 .

But there are still problems with the solution:

  • It does not recognize changes made in ExampleGameInEngineA (manual rebuild required)
  • The Condition when to add ExampleGameInEngineA is always met (because I have no good idea for a condition)

I am still open for better solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM