为了从 .net core webapi 项目的代码覆盖率中排除我的代码,我将该[System.Diagnostics.Analysis.ExcludeFromCodeCoverage]属性应用于不需要的类。
现在我想从我的代码覆盖范围中排除 Program.cs。但是,在 .net 6 中,我不知道如何向该文件应用属性,因为它没有类声明。谁能指导我如何将属性应用于此文件?
我一直假设顶级语句生成的类是一个隐藏的、不可访问的类。例如:
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。
但是,众所周知,您可以使用partial类Program来增强生成的类。修改代码以打印该字段...
System.Console.WriteLine(abc);
partial …Run Code Online (Sandbox Code Playgroud)