首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并表达式,在Fody中引起返回语句中的问题

合并表达式,在Fody中引起返回语句中的问题
EN

Stack Overflow用户
提问于 2017-08-15 21:09:21
回答 1查看 138关注 0票数 0

考虑以下代码片段:

代码语言:javascript
复制
private string GetFieldValueAsString(string nonAliasedEntityName = null, string nonAliasedName = null)
{
    return nonAliasedEntityName ?? nonAliasedName;   // simplified code of course!
}

它被编译成以下IL代码:

代码语言:javascript
复制
.method private hidebysig 
    instance string GetFieldValueAsString (
        [opt] string nonAliasedEntityName,
        [opt] string nonAliasedName
    ) cil managed 
{
    .param [1] = nullref
    .param [2] = nullref
    // Method begins at RVA 0x2cb6f
    // Code size 7 (0x7)
    .maxstack 8

    IL_0000: ldarg.1
    IL_0001: dup
    IL_0002: brtrue.s IL_0006

    IL_0004: pop
    IL_0005: ldarg.2

    IL_0006: ret
} // end of method MessageBuilder::GetFieldValueAsString

如果我在Fody中应用“修复返回”,我将得到以下IL代码:

代码语言:javascript
复制
.method private hidebysig 
    instance string GetFieldValueAsString (
        [opt] string nonAliasedEntityName,
        [opt] string nonAliasedName
    ) cil managed 
{
    .param [1] = nullref
    .param [2] = nullref
    // Method begins at RVA 0x2cb70
    // Code size 15 (0xf)
    .maxstack 3
    .locals (
        [0] string $returnVariable
    )

    IL_0000: ldarg.1
    IL_0001: dup
    IL_0002: dup
    IL_0003: stloc.0
    IL_0004: brtrue.s IL_000b

    IL_0006: pop
    IL_0007: ldarg.2
    IL_0008: stloc.0
    IL_0009: br.s IL_000b

    IL_000b: nop
    IL_000c: nop
    IL_000d: ldloc.0
    IL_000e: ret
} // end of method MessageBuilder::GetFieldValueAsString

在ILSpy中反编译时,它会给出以下错误,并且无法运行:

代码语言:javascript
复制
ICSharpCode.Decompiler.DecompilerException: Error decompiling System.String LinkDev.Notifications.Steps.MessageBuilder::GetFieldValueAsString(System.String,System.String)
 ---> System.Exception: Inconsistent stack size at IL_09
   at ICSharpCode.Decompiler.ILAst.ILAstBuilder.StackAnalysis(MethodDefinition methodDef)
   at ICSharpCode.Decompiler.ILAst.ILAstBuilder.Build(MethodDefinition methodDef, Boolean optimize, DecompilerContext context)
   at ICSharpCode.Decompiler.Ast.AstMethodBodyBuilder.CreateMethodBody(IEnumerable`1 parameters)
   at ICSharpCode.Decompiler.Ast.AstMethodBodyBuilder.CreateMethodBody(MethodDefinition methodDef, DecompilerContext context, IEnumerable`1 parameters)
   --- End of inner exception stack trace ---
   at ICSharpCode.Decompiler.Ast.AstMethodBodyBuilder.CreateMethodBody(MethodDefinition methodDef, DecompilerContext context, IEnumerable`1 parameters)
   at ICSharpCode.Decompiler.Ast.AstBuilder.CreateMethod(MethodDefinition methodDef)
   at ICSharpCode.Decompiler.Ast.AstBuilder.AddMethod(MethodDefinition method)
   at ICSharpCode.ILSpy.CSharpLanguage.DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options)
   at ICSharpCode.ILSpy.TextView.DecompilerTextView.DecompileNodes(DecompilationContext context, ITextOutput textOutput)
   at ICSharpCode.ILSpy.TextView.DecompilerTextView.<>c__DisplayClass31_0.<DecompileAsync>b__0()

我追踪到了堆栈的大小,它似乎还能保持。你知道是什么导致了这个问题吗?

更新1:

我为这个问题添加了一个临时的快速解决方案:

代码语言:javascript
复制
Instruction doubleDupInstruction = null;

for (var index = 0; index < instructions.Count; index++)
{
    var instruction = instructions[index];

    if (instruction.OpCode == OpCodes.Dup && instructions[index + 1].OpCode == OpCodes.Dup)
    {
        doubleDupInstruction = instructions[index + 1];
    }

    if (instruction.OpCode == OpCodes.Pop && doubleDupInstruction != null)
    {
        var extraPopInstruction = instructions[index];
        ilProcessor.Remove(extraPopInstruction);
        ilProcessor.InsertAfter(doubleDupInstruction, extraPopInstruction);
        doubleDupInstruction = null;
    }
}

到目前为止,它在一个相当大的程序中工作。我会继续监视它,如果有任何变化,我会更新。如果我能在“返修者”中找到问题的根源,那就好多了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-16 06:47:42

我认为到达IL_000b时堆栈大小不一致,这取决于来自IL0004还是IL0009

下面是我的分析,右边的值是执行相应行后的堆栈大小。

代码语言:javascript
复制
                            [0]
IL_0000: ldarg.1            [1]
IL_0001: dup                [2]
IL_0002: dup                [3]
IL_0003: stloc.0            [2]
IL_0004: brtrue.s IL_000b   [1]---
                                 |
IL_0006: pop                [0]  |
IL_0007: ldarg.2            [1]  |
IL_0008: stloc.0            [0]  |
IL_0009: br.s IL_000b       [0]  |
                                 v
IL_000b: nop                [?] arriving here is inconsistent
IL_000c: nop
IL_000d: ldloc.0
IL_000e: ret

也许您可以对此方法运行PEVerify并查看输出结果。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45701589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档