我正在对两个对象的不同实例之间的某些东西进行一些比较.因为这些比较是连续的(如果比较A==0,我们转到比较B,如果比较B==0,我们转到比较C,依此类推,直到我们返回a-1或a 1),并且我不想放太多的if,我想这样做:

public static class TStatsComparer
{
    public static int Compare(TStats? t1, TStats? other)
    {
        Func<TStats, TStats, int>[] comparisonRules = new Func<TStats, TStats, int>[]
            {
                (t1, t2) => CompareA(t1, t2),
                (t1, t2) => CompareB(t1, t2),
                (t1, t2) => CompareC(t1, t2),
                (t1, t2) => CompareD(t1, t2),
                (t1, t2) => CompareE(t1, t2),
            };

        int result = comparisonRules.Select(method => method(t1, other))
                            .FirstOrDefault(result => result != 0);

        return result;
}

其中CompareA(或B或C...)是如下方法

static int CompareA(TStats? t1, TStats? other)
{
    int result = 0;
    // ...
    // We do the comparison and store it in result
    // result values could be 0,1 or -1

    return result;
}

而被比较的班级

public class TStats : IComparable<TStats>
{
    //...

    public int CompareTo(TStats? other)
    {
        return TStatsComparer.Compare(this, other);
    }
}

因此,它得到第一个返回不同于0的值的比较,并返回该值.它按预期工作,但这里的问题是我不能从Visual Studio对其进行调试.例如,如果我将断点放在CompareA方法中,它会说

你的应用已进入中断状态,但当前没有所选调试引擎支持的代码正在执行(例如,只有本机运行时代码正在执行)

它仍然可以工作,但就是这样,我不能调试它.

如果我go 掉Func<>,直接顺序调用CompareA/B/C方法,我就可以正常调试它了.

我这样使用比较器是不是犯了很大的错误?

即使函数是在其他线程或某种匿名方法中执行的,我是否应该能够调试内部方法?

推荐答案

截至发帖时,这还不是一个真正的答案,但它可能是一个线索:

An issue that looks very similar was reported on GitHub in November 2021:
Debugger can't step into a function in a given situation with Linq #62232

问题记者声称,在使用.NET 6时,Windows 10上的VS Code和Visual Studio(2022)中会出现该问题;而在.NET 5和.NET Framework4.8中,该问题不会发生.

As of September 2023, the issue is still open.


中提供的用于娱乐的代码片段为:

static void Main(string[] args)
{
    var list = new[] { "1", "4", "h", "l1", "7" };
    var parsedList = list.Select(item => DoSomething(item)).Where(item => item == 1).ToList();
}

public static int DoSomething<T>(T input)
{
    return 1;
}

以及以下 comments :

Just place a breakpoint in DoSomething and run.

VS Goes into break mode: ("Your app has entered a break state, but no code is currently executing that is supported by the selected debug engine (e.g. only native runtime code is executing).") - stepping seems still to work, no other error is shown

VS Code : the callstack shows: "Error processing stackTrace request. Unknown Error 0x80131c49" - and seems to be completely stuck

Csharp相关问答推荐

减少Express Func T、bool断言中的公式中使用的条件运算符(4)数量(最多允许3个)

在Dapper中使用IasyncEum重写GetAsyncEum方法

. NET WireMock拒绝PostAsJsonAsync序列化

. net依赖注入如何避免服务类中的新

使用Audit.EntityFramework,我如何将外键的值设置为相关实体上的属性?

Azure Redis缓存与Entra ID身份验证

HttpConext.Request.Path和HttpConext.GetEndpoint()之间的差异

mocking对象的引发事件并作为用于调用方法的参数对象传递到那里

持有者安全定义未显示在Swagger.NET 8中

使用Entity Framework6在对象中填充列表会导致列表大多为空

如何在不复制或使用输出的情况下定义项目依赖

如何允许数组接受多个类型?

为什么我的伺服电机不动,下面的代码?

如何在.NET MAUI中最大化GraphicsView的大小?

当我try 在与XAMP的MySQL服务器连接的ASP.NET核心应用程序中添加迁移时出现错误

将J数组转换为列表,只保留一个嵌套的JToken

Visual Studio 17.8.0制表符自动完成问题--三缩进

Visual Studio如何使用当前的框架?

try 访问字典中的模拟对象时引发KeyNotFoundException

在.Net 8 Visual Studio 2022中启用本机AOT发布时发布失败