pbalaga
pbalaga

Reputation: 2068

How to get paths to all referenced DLLs in MSBuild?

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

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?

Upvotes: 2

Views: 2079

Answers (2)

pbalaga
pbalaga

Reputation: 2068

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.

Upvotes: 7

C.J.
C.J.

Reputation: 16081

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()

Upvotes: 1

Related Questions