相关疑难解决方法(0)

从 .net 6 中的代码覆盖率中排除 Program.cs

为了从 .net core webapi 项目的代码覆盖率中排除我的代码,我将该[System.Diagnostics.Analysis.ExcludeFromCodeCoverage]属性应用于不需要的类。

现在我想从我的代码覆盖范围中排除 Program.cs。但是,在 .net 6 中,我不知道如何向该文件应用属性,因为它没有类声明。谁能指导我如何将属性应用于此文件?

c# asp.net-web-api asp.net-core .net-6.0

13
推荐指数
1
解决办法
1万
查看次数

什么情况下生成的Top Level Statement类会是$Program?

我一直假设顶级语句生成的类是一个隐藏的、不可访问的类。例如:

System.Console.WriteLine(2);

partial class Program
{
    public static string abc = "def";
}
Run Code Online (Sandbox Code Playgroud)

当使用默认分支“C# 9:顶级语句(2020 年 5 月 27 日)”分支在 SharpLab.io 中运行时,生成的 C# 将是

// [ ... using and assembly attributes ... ]
internal static class $Program
{
    private static void $Main(string[] args)
    {
        Console.WriteLine(2);
    }
}
internal class Program
{
    public static string abc = "def";
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,默认分支发出<Program>$not$Program<Main>$not $Main

但是,众所周知,您可以使用partialProgram来增强生成的类。修改代码以打印该字段...

System.Console.WriteLine(abc);
partial …
Run Code Online (Sandbox Code Playgroud)

c# c#-9.0 toplevel-statement

7
推荐指数
1
解决办法
639
查看次数