我有一段与此类似的代码:
dynamic a = new ValueHolder();
dynamic b = new ValueHolder();
dynamic c = new ValueHolder();
a.MtdActual = b.MtdActual + c.MtdActual;
a.YtdActual = b.YtdActual + c.YtdActual;
a.MtdVariance = b.MtdVariance + c.MtdVariance;
我创建了这个例子,根据代码分析,那段代码的圈复杂度是 25。
在 IL spy 中反汇编代码时,您可以看到:
object a = new ValueHolder();
object b = new ValueHolder();
object c = new ValueHolder();
if (Program.<ConsumeA>o__SiteContainer2.<>p__Site3 == null)
{
Program.<ConsumeA>o__SiteContainer2.<>p__Site3 = CallSite<Func<CallSite, object, object, object>>.Create(Binder.SetMember(CSharpBinderFlags.None, "MtdActual", typeof(Program), new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}));
}
Func<CallSite, object, object, object> arg_159_0 = Program.<ConsumeA>o__SiteContainer2.<>p__Site3.Target;
CallSite arg_159_1 = Program.<ConsumeA>o__SiteContainer2.<>p__Site3;
object arg_159_2 = a;
if (Program.<ConsumeA>o__SiteContainer2.<>p__Site4 == null)
{
Program.<ConsumeA>o__SiteContainer2.<>p__Site4 = CallSite<Func<CallSite, object, object, object>>.Create(Binder.BinaryOperation(CSharpBinderFlags.None, ExpressionType.Add, typeof(Program), new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}));
}
...
我的问题显然是 if else 和编译器生成的逻辑正在增加复杂性。但是,如果可能的话,我如何将代码分析配置为不在此级别上运行,而是在更简单的编码(预编译)阶段作为第一个代码片段进行评估?