如何使用官方的C# SDK获取NuGet包的发布说明?

huangapple go评论87阅读模式
英文:

How can i get the release notes of a NuGet package with the official SDK in C#

问题

我需要获取存储在元数据中的发布说明。这段代码似乎能够读取元数据,但我不知道如何获取发布说明。

using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;

ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;

SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>();

IEnumerable<IPackageSearchMetadata> packages = await resource.GetMetadataAsync(
    "Flexygo",
    includePrerelease: true,
    includeUnlisted: false,
    cache,
    logger,
    cancellationToken);

我正在尝试只使用官方 SDK 获取包含发布说明的字符串。

英文:

I need to get the Release Notes stored in the metadata. This code seems to be able to read the metadata, however i dont know how to get to the release notes.

using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;


ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;

SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3(&quot;https://api.nuget.org/v3/index.json&quot;);
PackageMetadataResource resource = await repository.GetResourceAsync&lt;PackageMetadataResource&gt;();

IEnumerable&lt;IPackageSearchMetadata&gt; packages = await resource.GetMetadataAsync(
    &quot;Flexygo&quot;,
    includePrerelease: true,
    includeUnlisted: false,
    cache,
    logger,
    cancellationToken);

i am trying to get a String with the release notes using only the official SDK

答案1

得分: 2

你可以使用库NuGet.Client来实现上述操作。通过以下代码,可以使用NuGet包管理器控制台安装以下库:

Install-Package NuGet.Client

如果不想这样做,你也可以根据以下方式更改你自己的代码以打印提取的所有信息:

using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using System.Reflection;

class Program
{
    static async Task Main()
    {
        ILogger logger = NullLogger.Instance;
        CancellationToken cancellationToken = CancellationToken.None;

        SourceCacheContext cache = new SourceCacheContext();
        SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
        PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>();

        IEnumerable<IPackageSearchMetadata> packages = await resource.GetMetadataAsync(
            "Flexygo",
            includePrerelease: true,
            includeUnlisted: false,
            cache,
            logger,
            cancellationToken);

        foreach (var package in packages)
        {
            var packageMetadata = await resource.GetMetadataAsync(package.Identity, cache, logger, cancellationToken);
            var v = await packageMetadata.GetDeprecationMetadataAsync();
            Console.WriteLine("------------------------------------------------------------------");
            Console.WriteLine("Package Metadata: " + packageMetadata.Title.ToString());

            Type t = packageMetadata.GetType();
            PropertyInfo[] pi = t.GetProperties();
            foreach (PropertyInfo p in pi)
            {
                System.Console.WriteLine(p.Name + " : " + p.GetValue(packageMetadata));
            }
            Console.WriteLine("------------------------------------------------------------------");
        }
    }
}

如何使用官方的C# SDK获取NuGet包的发布说明?

英文:

You can achieve the above using the Library NuGet.Client. Install the following library using your Nuget package manager console via the below code :

Install-Package NuGet.Client

If not you can alter your own code as per below to print all information extracted :

using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using System.Reflection;

class Program
{
    static async Task Main()
    {
        ILogger logger = NullLogger.Instance;
        CancellationToken cancellationToken = CancellationToken.None;

        SourceCacheContext cache = new SourceCacheContext();
        SourceRepository repository = Repository.Factory.GetCoreV3(&quot;https://api.nuget.org/v3/index.json&quot;);
        PackageMetadataResource resource = await repository.GetResourceAsync&lt;PackageMetadataResource&gt;();

        IEnumerable&lt;IPackageSearchMetadata&gt; packages = await resource.GetMetadataAsync(
            &quot;Flexygo&quot;,
            includePrerelease: true,
            includeUnlisted: false,
            cache,
            logger,
            cancellationToken);


        foreach (var package in packages)
        {
            var packageMetadata = await resource.GetMetadataAsync(package.Identity, cache, logger, cancellationToken);
            var v = await packageMetadata.GetDeprecationMetadataAsync();
            Console.WriteLine(&quot;------------------------------------------------------------------&quot;);
            Console.WriteLine(&quot;Package Metadata: &quot; + packageMetadata.Title.ToString());

            Type t = packageMetadata.GetType();
            PropertyInfo[] pi = t.GetProperties();
            foreach (PropertyInfo p in pi)
            {
                System.Console.WriteLine(p.Name + &quot; : &quot; + p.GetValue(packageMetadata));
            }
            Console.WriteLine(&quot;------------------------------------------------------------------&quot;);
        }
    }
}

如何使用官方的C# SDK获取NuGet包的发布说明?

huangapple
  • 本文由 发表于 2023年7月3日 17:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定