Home > Net >  Determine PackageReference items programmatically
Determine PackageReference items programmatically

Time:08-24

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?

CodePudding user response:

AppDomain.CurrentDomain.GetAssemblies() - Should help

you can get package version ImageRuntimeVersion

CodePudding user response:

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.

I guess the solution is to parse the XML, or rely on a dotnet tool of some kind.

  • Related