Piedone
Piedone

Reputation: 2888

Using the same assembly references in csproj with different folder structures

I have a C# project, MyProject.csproj that sits in a solution with the following folder structure and references Dependency.dll:

Thus the reference to Dependency.dll in MyProject.csproj has a HintPath of something like this:

..\..\Libs\Dependency.dll

Now I'd like to use MyProject in a different solution in a different project structure, without modifications, as source. This is because MyProject sits in its own source control repository and I'm using it in different solutions as Mercurial subrepositories/Git submodules. (The problem might be solved on the source control level...) Such a diffreent solution would look like this:

Note that the MyProject folder is now on the same level as the Libs folder. Thus the original HintPath is now invalid (since it should be ..\Libs\Dependency.dll) and I get build errors.

Is there a way to fix this but keep the same csproj across the different solutions?

I found the following possible solutions which are great but require the modification of the csproj. This is mostly possible in my case but sometimes there are external components where I can't request such modifications, so I'd look for some solution-level override if possible.

Thank you.

Upvotes: 0

Views: 1648

Answers (1)

Piedone
Piedone

Reputation: 2888

For now, I solved the issue using the technique outlined in this blog post.

<ItemGroup>
  <LibReferenceSearchPathFiles Include="..\..\Libs\**\*.dll">
      <InProject>false</InProject>
  </LibReferenceSearchPathFiles>
</ItemGroup>
<Target Name="BeforeResolveReferences">
  <RemoveDuplicates Inputs="@(LibReferenceSearchPathFiles->'%(RootDir)%(Directory)')">
    <Output TaskParameter="Filtered" ItemName="LibReferenceSearchPath" />
  </RemoveDuplicates>
  <CreateProperty Value="@(LibReferenceSearchPath);$(AssemblySearchPaths)">
    <Output TaskParameter="Value" PropertyName="AssemblySearchPaths" />
  </CreateProperty>
</Target>

This enables dlls from subfolders of Libs to be loaded. If all the dlls would be in the root of the Libs folder, then the first wildcard can be removed from the Include value.

Upvotes: 2

Related Questions