1

I would like to get paths to all assemblies referenced in a .csproj given the following requirements:

  • include assemblies no matter how they were referenced (project, nuget, direct .dll import)
  • do not trigger a build

There is a good answer about how to do it for project references:

<MSBuild Projects="@(ProjectReference)" Targets="GetTargetPath">
  <Output TaskParameter="TargetOutputs" ItemName="MyReferencedAssemblies" />
</MSBuild>

Is there a similar way to get .dll paths of all other types of references?

pbalaga
  • 2,018
  • 1
  • 21
  • 33

2 Answers2

6

There is seems to be a clean way to do it:

  <Target Name="GatherReferences" DependsOnTargets="ResolveReferences">
    <ItemGroup>
      <MyReferencedAssemblies Include="@(ReferencePath)" />
    </ItemGroup>
  </Target>

After that MyReferencedAssemblies item group contains a collection of all referenced DLLs (full paths, all kinds). It also works for PackageReference imports in the new .csproj format. Important part is that @(ReferencePath) is non-empty only after ResolveReferences finishes.

pbalaga
  • 2,018
  • 1
  • 21
  • 33
1

It doesn't look like TargetOutput's will give you want you want, since you are looking for inputs.

This should be a trivial matter if you use the MSBuild API (using c#). You can can see how I extended the Project class to include just such a thing:

https://github.com/chris1248/SolutionBuilder/blob/master/MSBuildTools/ProjectBase.cs

specifically look at the function:

protected int GatherReferenceAssemblies()
C.J.
  • 15,637
  • 9
  • 61
  • 77
  • Thank you for the hints. In fact I could use MSBuild API, as I pass gathered referenced assemblies into a custom `Task`. I could use the API from there directly. Anyway, my reasoning is: since MSBuild must know these references to build a project, there should be a way to access it. [I found a way](https://stackoverflow.com/a/50611221/318795), which seems to enable this. – pbalaga May 30 '18 at 18:24