Search code examples
c#.netreflectionnuget

Determine PackageReference items programmatically


My .NET Core 6 project's .csproj has this:

<ItemGroup>
  <PackageReference Include="Foo" Version="1.2.3" />
  <PackageReference Include="Bar" Version="4.5.6" />
</ItemGroup>

How can I determine programmatically (at runtime) that the project references the "Foo" and "Bar" nuget packages, and also determine their versions?

I tried, without success:

  • Assembly.GetExecutingAssembly().GetReferencedAssemblies()
  • Assembly.GetEntryAssembly().GetReferencedAssemblies()
  • AppDomain.CurrentDomain.GetAssemblies()

(I'm specifically interested in the NuGet.CommandLine package, in case that makes any difference.)

UPDATE
I think the problem is the NuGet.CommandLine package. I don't think it's loaded into the appdomain. How can I detect it?


Solution

  • I suspect the problem is the package's NuGet.CommandLine.nuspec has this:

    <developmentDependency>true</developmentDependency>
    

    Which comes from here.

    So it's not included as a runtime dependency, thus not copied into the bin/Debug/net6.0 directory, thus not loaded into the AppDomain upon app start.