https://github.com/yanckst/search-nuget-v3/blob/master/search.linq
<Query Kind="Program">
<NuGetReference>NuGet.PackageManagement</NuGetReference>
<NuGetReference>NuGet.Packaging</NuGetReference>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>NuGet.Protocol.Core.Types</Namespace>
<Namespace>NuGet.Configuration</Namespace>
<Namespace>NuGet.ProjectManagement</Namespace>
<Namespace>NuGet.PackageManagement</Namespace>
</Query>
NuGet.Protocol.Core.Types.SourceRepository sourceRepository;
async Task Main()
{
InitializeNugetDependencies();
var jsonNugetPackages = await SearchTestPackage("Newtonsoft.Json");
jsonNugetPackages.Dump();
}
private void InitializeNugetDependencies()
{
List<Lazy<INuGetResourceProvider>> providers = new List<Lazy<INuGetResourceProvider>>();
providers.AddRange(Repository.Provider.GetCoreV3());
PackageSource packageSource = new PackageSource("https://api.nuget.org/v3/index.json");
this.sourceRepository = new NuGet.Protocol.Core.Types.SourceRepository(packageSource, providers);
}
private async Task<IEnumerable<IPackageSearchMetadata>> SearchTestPackage(string packageName)
{
var searchResource = await this.sourceRepository.GetResourceAsync<PackageSearchResource>();
var supportedFramework = new[] { ".NETFramework,Version=v4.6" };
var searchFilter = new SearchFilter(true)
{
SupportedFrameworks = supportedFramework,
IncludeDelisted = false
};
return await searchResource.SearchAsync(packageName, searchFilter, 0, 10, null, CancellationToken.None);
}
https://www.cnblogs.com/smartisn/p/16026342.html
using System;
using static NuGet.Frameworks.FrameworkConstants;
using static NuGet.Protocol.Core.Types.Repository;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
//using NuGet.Test.Utility;
using NuGet.Versioning;
using NuGet.PackageManagement;
namespace NuGet_Remote_develop
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine("!!!!");
// Set up
var packageIdentityA100 = new PackageIdentity("a", NuGetVersion.Parse("1.0.0"));
Console.WriteLine("%%%%%");
var packageIdentityB100 = new PackageIdentity("b", NuGetVersion.Parse("1.0.0"));
var packageIdentityC100 = new PackageIdentity("c", NuGetVersion.Parse("1.0.0"));
var packageIdentityC110 = new PackageIdentity("c", NuGetVersion.Parse("1.1.0"));
var packageIdentityD100 = new PackageIdentity("d", NuGetVersion.Parse("1.0.0"));
var packageDependencyInfos = new List<PackageDependencyInfo>();
//制造关于A的依赖图
var packageDependencyInfoA = new PackageDependencyInfo(packageIdentityA100,
new PackageDependency[] {
new PackageDependency(packageIdentityB100.Id, VersionRange.Parse(packageIdentityB100.Version.OriginalVersion)),
new PackageDependency(packageIdentityC100.Id, VersionRange.Parse(packageIdentityC100.Version.OriginalVersion)),
});
var packageDependencyInfoB = new PackageDependencyInfo(packageIdentityB100,
new PackageDependency[] {
new PackageDependency(packageIdentityD100.Id, VersionRange.Parse(packageIdentityD100.Version.OriginalVersion)),
});
var packageDependencyInfoC = new PackageDependencyInfo(packageIdentityC110, Enumerable.Empty<PackageDependency>());
var packageDependencyInfoD = new PackageDependencyInfo(packageIdentityD100, Enumerable.Empty<PackageDependency>());
//增加所有的依赖关系图
packageDependencyInfos.Add(packageDependencyInfoA);
packageDependencyInfos.Add(packageDependencyInfoB);
packageDependencyInfos.Add(packageDependencyInfoC);
packageDependencyInfos.Add(packageDependencyInfoD);
// Act————》》获取依赖图
var packageWithDependants = PackageGraphAnalysisUtilities.GetPackagesWithDependants(packageDependencyInfos);
Console.WriteLine(packageWithDependants);
Console.WriteLine("*****");
foreach (var package in packageWithDependants)
{
switch (package.Identity.Id)
{
case "a":
{
//看他是不是没有其他的依赖项
Console.WriteLine(package.DependantPackages.Count);
Console.WriteLine(package.IsTopLevelPackage);
break;
}
case "b":
{
//看他是不是只有一个依赖项,而且这个依赖项是A。并且他不是直接依赖
Console.WriteLine( package.DependantPackages.Count);
Console.WriteLine(package.DependantPackages.Single().Id);
Console.WriteLine(package.IsTopLevelPackage);
break;
}
case "c":
{
Console.WriteLine(package.DependantPackages.Count);
Console.WriteLine(package.IsTopLevelPackage);
break;
}
case "d":
{
Console.WriteLine( package.DependantPackages.Count);
Console.WriteLine( package.DependantPackages.Single().Id);
Console.WriteLine(package.IsTopLevelPackage);
break;
}
default:
{
Console.WriteLine($"Unexpected package {package.Identity}");
break;
}
}
}
Console.ReadKey();
}
}
}