小编cub*_*e45的帖子

创建空的 IAsyncEnumerable

我有一个这样写的界面:

public interface IItemRetriever
{
    public IAsyncEnumerable<string> GetItemsAsync();
}
Run Code Online (Sandbox Code Playgroud)

我想编写一个不返回任何项目的空实现,如下所示:

public class EmptyItemRetriever : IItemRetriever
{
    public IAsyncEnumerable<string> GetItemsAsync()
    {
       // What do I put here if nothing is to be done?
    }
}
Run Code Online (Sandbox Code Playgroud)

如果它是一个普通的 IEnumerable,我会return Enumerable.Empty<string>();,但我没有找到任何AsyncEnumerable.Empty<string>().

解决方法

我发现这有效但很奇怪:

public async IAsyncEnumerable<string> GetItemsAsync()
{
    await Task.CompletedTask;
    yield break;
}
Run Code Online (Sandbox Code Playgroud)

任何的想法?

c# c#-8.0 iasyncenumerable

34
推荐指数
3
解决办法
6242
查看次数

如何在没有 .nuspec 的情况下将文件添加到 csproj NuGet 包中的 build 文件夹

我已经为我的通用项目(它是一个 NuGet 包)安装了 AsyncUsageAnalyzers,并且我希望在依赖它的所有包/应用程序中强制执行这些规则。我更改了UseConfigureAwait规则的严重性,并创建了一个 .ruleset。

我在/sf/answers/1419023091/上读到我需要一个具有给定结构的 NuGet 包。该断言已在此处得到确认/sf/answers/3228079641/

我现在所拥有的:

构建/Common.ruleset

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="My common analyzer rules" Description="The rules that are enforced on all of my projects" ToolsVersion="10.0">
  <Rules AnalyzerId="AsyncUsageAnalyzers" RuleNamespace="AsyncUsageAnalyzers">
    <Rule Id="UseConfigureAwait" Action="Error" />
  </Rules>
</RuleSet>
Run Code Online (Sandbox Code Playgroud)

构建/My.Package.Id.targets

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)Common.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

在我的 .csproj

<Import Project="build\My.Package.Id.targets" />
Run Code Online (Sandbox Code Playgroud)

我的问题是:既然我已经这样做了并且规则已正确应用于我的项目,我该如何build/在 NuGet 包内的build/文件夹内添加文件?我知道我可以用链接中提到的 .nuspec 文件来做到这一点,但我想用新的 .csproj 语法来做到这一点。

csproj nuget

4
推荐指数
1
解决办法
4680
查看次数

标签 统计

c# ×1

c#-8.0 ×1

csproj ×1

iasyncenumerable ×1

nuget ×1