简体   繁体   English

以编程方式确定 PackageReference 项

[英]Determine PackageReference items programmatically

My .NET Core 6 project's .csproj has this:我的 .NET Core 6 项目的.csproj有这个:

<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?如何以编程方式(在运行时)确定项目引用“Foo”和“Bar”nuget 包,并确定它们的版本?

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.) (我对NuGet.CommandLine package 特别感兴趣,以防万一。)

UPDATE更新
I think the problem is the NuGet.CommandLine package.我认为问题是NuGet.CommandLine package。 I don't think it's loaded into the appdomain.我不认为它已加载到 appdomain 中。 How can I detect it?我怎样才能检测到它?

AppDomain.CurrentDomain.GetAssemblies() - Should help AppDomain.CurrentDomain.GetAssemblies() - 应该有帮助

you can get package version ImageRuntimeVersion您可以获得 package 版本ImageRuntimeVersion

I suspect the problem is the package's NuGet.CommandLine.nuspec has this:我怀疑问题是包的NuGet.CommandLine.nuspec有这个:

<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.所以它不包含在运行时依赖中,因此不会复制到bin/Debug/net6.0目录中,因此不会在应用启动时加载到 AppDomain 中。

I guess the solution is to parse the XML, or rely on a dotnet tool of some kind.我想解决方案是解析 XML,或者依赖某种dotnet tool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM