我正在对两个对象的不同实例之间的某些东西进行一些比较.因为这些比较是连续的(如果比较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
方法,我就可以正常调试它了.
我这样使用比较器是不是犯了很大的错误?
即使函数是在其他线程或某种匿名方法中执行的,我是否应该能够调试内部方法?