从异步方法 return 列表的最佳方法是什么?

What is the best way to return a list from an Async method?

我正在使用此示例编写 RSS reader https://github.com/dotnet/SyndicationFeedReaderWriter

我想将所有 ISyndicationItem 记录到 return 作为列表。我得到的编译器错误是“错误 CS1983 async 方法的 return 类型必须是 voidTaskTask<T>,类似任务输入 IAsyncEnumerable<T>IAsyncEnumerator<T>" 最理想的方法是什么?

using Microsoft.SyndicationFeed;
using Microsoft.SyndicationFeed.Rss;
using System.Collections.Generic;
using System.Xml;

static async List<ISyndicationItem> ReadRSSFeed(string inputUri)
{
    var itemList = new List<ISyndicationItem>();
    using (var xmlReader = XmlReader.Create(inputUri,
        new XmlReaderSettings() { Async = true }))
    {
        var feedReader = new RssFeedReader(xmlReader);

        while (await feedReader.Read())
        {
            switch (feedReader.ElementType)
            {
                // Read category
                case SyndicationElementType.Category:
                    ISyndicationCategory category = await feedReader.ReadCategory();
                    break;

                // Read Image
                case SyndicationElementType.Image:
                    ISyndicationImage image = await feedReader.ReadImage();
                    break;

                // Read Item
                case SyndicationElementType.Item:
                    ISyndicationItem item = await feedReader.ReadItem();
                    itemList.Add(item);
                    break;

                // Read link
                case SyndicationElementType.Link:
                    ISyndicationLink link = await feedReader.ReadLink();
                    break;

                // Read Person
                case SyndicationElementType.Person:
                    ISyndicationPerson person = await feedReader.ReadPerson();
                    break;

                // Read content
                default:
                    ISyndicationContent content = await feedReader.ReadContent();
                    break;
            }
        }
    }
    return itemList;
}

只是 return Task<T> 而不是 T。在该方法中,您仍然 return T.

的一个实例

所以应该是:

static async Task<List<ISyndicationItem>> ReadRSSFeed(string inputUri)