首页 新闻 搜索 专区 学院

MSIL问题 Inconsistent stack size at IL_7B

0
悬赏园豆:100 [待解决问题]

报错信息如下:

ICSharpCode.Decompiler.DecompilerException: Error decompiling System.Void AOPTest.Program::aa1()
 ---> System.Exception: Inconsistent stack size at IL_7B
   在 ICSharpCode.Decompiler.ILAst.ILAstBuilder.StackAnalysis(MethodDefinition methodDef)
   在 ICSharpCode.Decompiler.ILAst.ILAstBuilder.Build(MethodDefinition methodDef, Boolean optimize, DecompilerContext context)
   在 ICSharpCode.Decompiler.Ast.AstMethodBodyBuilder.CreateMethodBody(IEnumerable`1 parameters)
   在 ICSharpCode.Decompiler.Ast.AstMethodBodyBuilder.CreateMethodBody(MethodDefinition methodDef, DecompilerContext context, IEnumerable`1 parameters)
   --- 内部异常堆栈跟踪的结尾 ---
   在 ICSharpCode.Decompiler.Ast.AstMethodBodyBuilder.CreateMethodBody(MethodDefinition methodDef, DecompilerContext context, IEnumerable`1 parameters)
   在 ICSharpCode.Decompiler.Ast.AstBuilder.CreateMethod(MethodDefinition methodDef)
   在 ICSharpCode.Decompiler.Ast.AstBuilder.AddMethod(MethodDefinition method)
   在 ICSharpCode.ILSpy.CSharpLanguage.DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options)
   在 ICSharpCode.ILSpy.TreeNodes.MethodTreeNode.Decompile(Language language, ITextOutput output, DecompilationOptions options)
   在 ICSharpCode.ILSpy.TextView.DecompilerTextView.DecompileNodes(DecompilationContext context, ITextOutput textOutput)
   在 ICSharpCode.ILSpy.TextView.DecompilerTextView.<>c__DisplayClass17.<DecompileAsync>b__16()

MSIL代码如下:

.method private hidebysig static 
    void aa1 () cil managed 
{
    .custom instance void AOPTest.MethodInterceptAttribute::.ctor() = (
        01 00 00 00
    )
    // Method begins at RVA 0x233c
    // Code size 128 (0x80)
    .maxstack 6
    .locals (
        [0] class [mscorlib]System.Reflection.MemberInfo,
        [1] class AOPTest.MethodInterceptAttribute,
        [2] class [AOP]AOP.MethodExecutionEventArgs,
        [3] class [mscorlib]System.Exception,
        [4] class [mscorlib]System.Type[],
        [5] object[],
        [6] bool
    )

    IL_0000: nop
    IL_0001: ldtoken AOPTest.Program
    IL_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    IL_000b: ldstr "aa1"
    IL_0010: ldc.i4.s 60
    IL_0012: ldnull
    IL_0013: ldc.i4 0
    IL_0018: newarr [mscorlib]System.Type
    IL_001d: stloc.s 4
    IL_001f: ldloc.s 4
    IL_0021: ldnull
    IL_0022: call instance class [mscorlib]System.Reflection.MethodInfo [mscorlib]System.Type::GetMethod(string, valuetype [mscorlib]System.Reflection.BindingFlags, class [mscorlib]System.Reflection.Binder, class [mscorlib]System.Type[], valuetype [mscorlib]System.Reflection.ParameterModifier[])
    IL_0027: stloc.s 0
    IL_0029: ldloc.s 0
    IL_002b: ldtoken AOPTest.MethodInterceptAttribute
    IL_0030: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    IL_0035: ldc.i4.0
    IL_0036: callvirt instance object[] [mscorlib]System.Reflection.MemberInfo::GetCustomAttributes(class [mscorlib]System.Type, bool)
    IL_003b: ldc.i4.0
    IL_003c: ldelem.ref
    IL_003d: isinst AOPTest.MethodInterceptAttribute
    IL_0042: stloc.s 1
    IL_0044: ldloc.s 0
    IL_0046: ldnull
    IL_0047: ldc.i4 0
    IL_004c: newarr [mscorlib]System.Type
    IL_0051: stloc.s 5
    IL_0053: ldloc.s 5
    IL_0055: ldloc.s 5
    IL_0057: ldnull
    IL_0058: newobj instance void [AOP]AOP.MethodExecutionEventArgs::.ctor(class [mscorlib]System.Reflection.MemberInfo, object, object[], string)
    IL_005d: stloc.s 2
    IL_005f: nop
    IL_0060: ldloc.s 1
    IL_0062: ldloc.s 2
    IL_0064: callvirt instance bool [AOP]AOP.IMethodInject::Executeing(class [AOP]AOP.MethodExecutionEventArgs)
    IL_0069: ldc.i4.0
    IL_006a: ceq
    IL_006c: stloc.s 6
    IL_006e: ldloc.s 6
    IL_0070: brtrue.s IL_007c

    IL_0072: nop
    .try
    {
        IL_0073: nop
        IL_0074: nop
        IL_0075: leave.s IL_007b
    } // end .try
    catch [mscorlib]System.Exception
    {
        IL_0077: stloc.s 3
        IL_0079: leave.s IL_007b
    } // end handler

    IL_007b: nop

    IL_007c: nop
    IL_007d: br.s IL_007f

    IL_007f: ret
} // end of method Program::aa1

我单独把try-catch去掉不会报错,单独把if去掉也不会报错

问题补充:

源代码:

var il = method.Body.GetILProcessor();
var module = method.Module;

var newmethod = CompilerCopyMethod(method, module);

mtype.Methods.Add(newmethod);

method.Body.Instructions.Clear();
method.Body.ExceptionHandlers.Clear();
method.Body.Variables.Clear();
method.Body.Instructions.Add(il.Create(OpCodes.Nop));

#region 定义变量并且赋值
var varMethodBase = new VariableDefinition(module.Import(typeof(System.Reflection.MemberInfo)));
method.Body.Variables.Add(varMethodBase);

var varIMethodInject = new VariableDefinition(methodInject.AttributeType);
method.Body.Variables.Add(varIMethodInject);

var varMethodExecutionEventArgs = new VariableDefinition(module.Import(typeof(MethodExecutionEventArgs)));
method.Body.Variables.Add(varMethodExecutionEventArgs);

var varexception = new VariableDefinition(module.Import(typeof(System.Exception)));
method.Body.Variables.Add(varexception);

var vartypeArray = new VariableDefinition(module.Import(typeof(Type[])));
method.Body.Variables.Add(vartypeArray);
method.Body.InitLocals = false;

il.Append(new[] { 
    il.Create(OpCodes.Ldtoken, mtype),
    il.Create(OpCodes.Call, module.Import(GetTypeFromHandle())),
    il.Create(OpCodes.Ldstr, method.Name),
    il.Create(OpCodes.Ldc_I4_S, (sbyte)60),
    il.Create(OpCodes.Ldnull),
    il.Create(OpCodes.Ldc_I4, method.Parameters.Count),
    il.Create(OpCodes.Newarr, module.Import(typeof(System.Type))),
    il.Create(OpCodes.Stloc_S, vartypeArray),
    il.Create(OpCodes.Ldloc_S, vartypeArray)
});

method.Parameters.ForEach((p, i) => il.Append(new[] { 
    il.Create(OpCodes.Ldc_I4, i), 
    il.Create(OpCodes.Ldtoken, p.ParameterType),
    il.Create(OpCodes.Call, module.Import(GetTypeFromHandle())),
    il.Create(OpCodes.Stelem_Ref), 
    il.Create(OpCodes.Ldloc_S, vartypeArray)
}));

il.Append(new[] {
    il.Create(OpCodes.Ldnull),
    il.Create(OpCodes.Call, module.Import(GetMethod(new Type[]{typeof(string), typeof(BindingFlags), typeof(Binder), typeof(Type[]), typeof(ParameterModifier[]) }))),
    il.Create(OpCodes.Stloc_S, varMethodBase),
    il.Create(OpCodes.Ldloc_S, varMethodBase)
});

il.Append(new[] { 
    il.Create(OpCodes.Ldtoken, methodInject.AttributeType),
    il.Create(OpCodes.Call, module.Import(GetTypeFromHandle())),
    il.Create(OpCodes.Ldc_I4_0),
    il.Create(OpCodes.Callvirt, module.Import(GetCustomAttributes())),
    il.Create(OpCodes.Ldc_I4_0), 
    il.Create(OpCodes.Ldelem_Ref),
    il.Create(OpCodes.Isinst, methodInject.AttributeType),
    il.Create(OpCodes.Stloc_S, varIMethodInject)
});

var thisIl = method.IsStatic ? il.Create(OpCodes.Ldnull) : il.Create(OpCodes.Ldarg_S, method.Body.ThisParameter);

var varparams = new VariableDefinition(module.Import(typeof(object[])));
method.Body.Variables.Add(varparams);

il.Append(new[] { 
    il.Create(OpCodes.Ldloc_S, varMethodBase),
    thisIl,
    il.Create(OpCodes.Ldc_I4, method.Parameters.Count),
    il.Create(OpCodes.Newarr, module.Import(typeof(System.Type))),
    il.Create(OpCodes.Stloc_S, varparams),
    il.Create(OpCodes.Ldloc_S, varparams)
});

method.Parameters.ForEach((p, i) => il.Append(new[] { 
    il.Create(OpCodes.Ldc_I4, i++), 
    il.Create(OpCodes.Ldarg_S, p),
    il.Create(OpCodes.Box, p.ParameterType), 
    il.Create(OpCodes.Stelem_Ref),
    il.Create(OpCodes.Ldloc_S, varparams)
}));

il.Append(il.Create(OpCodes.Ldloc_S, varparams));

if (method.ReturnType.IsValueType)
    il.Append(il.Create(OpCodes.Ldstr, method.ReturnType.FullName));
else
    il.Append(il.Create(OpCodes.Ldnull));

il.Append(new[] { 
    il.Create(OpCodes.Newobj, module.Import(typeof(MethodExecutionEventArgs).GetConstructor(
                new[] {
                    typeof(MethodBase), 
                    typeof(object), 
                    typeof(object[]),
                    typeof(string)
                }))), 
    il.Create(OpCodes.Stloc_S, varMethodExecutionEventArgs)
});

#endregion

#region If

var varflag = new VariableDefinition(module.Import(typeof(bool)));
method.Body.Variables.Add(varflag);

var endIf = (method.ReturnType.FullName != "System.Void") ? il.Create(OpCodes.Ldloc_S, varMethodExecutionEventArgs) : il.Create(OpCodes.Nop);

il.Append(new[] { 
    il.Create(OpCodes.Nop), 
    il.Create(OpCodes.Ldloc_S, varIMethodInject),
    il.Create(OpCodes.Ldloc_S, varMethodExecutionEventArgs),
    il.Create(OpCodes.Callvirt, module.Import(typeof(IMethodInject).GetMethod("Executeing", new[] { typeof(MethodExecutionEventArgs) }))),
    il.Create(OpCodes.Ldc_I4_0), 
    il.Create(OpCodes.Ceq), 
    il.Create(OpCodes.Stloc_S, varflag),
    il.Create(OpCodes.Ldloc_S, varflag), 
    il.Create(OpCodes.Brtrue_S, endIf),
    il.Create(OpCodes.Nop)
});

#endregion

#region Try

//var varflag = new VariableDefinition(module.Import(typeof(bool)));
//method.Body.Variables.Add(varflag);
//var varflag1 = new VariableDefinition(module.Import(typeof(bool)));
//method.Body.Variables.Add(varflag1);
//var endIf = il.Create(OpCodes.Nop);
//il.Append(new[] { 
//    il.Create(OpCodes.Nop),
//    il.Create(OpCodes.Ldc_I4_1),
//    il.Create(OpCodes.Stloc_1),
//    il.Create(OpCodes.Ldloc_1),
//    il.Create(OpCodes.Ldc_I4_1),
//    il.Create(OpCodes.Ceq),
//    il.Create(OpCodes.Stloc_2),
//    il.Create(OpCodes.Ldloc_2),
//    il.Create(OpCodes.Brtrue_S, endIf),
//    il.Create(OpCodes.Nop)
//});

var endTry = il.Create(OpCodes.Nop);
var trySatrt = il.Create(OpCodes.Nop);
var tryEnd = il.Create(OpCodes.Stloc_S, varexception);

il.Append(new[] { 
    trySatrt,
    il.Create(OpCodes.Nop),
    il.Create(OpCodes.Leave_S, endTry), 
    tryEnd, 
    il.Create(OpCodes.Leave_S, endTry)
});

#endregion

#region End
il.Append(endTry);
il.Append(il.Create(OpCodes.Nop));
il.Append(endIf);

var ret = il.Create(OpCodes.Ret);
il.Append(il.Create(OpCodes.Br_S, ret));

if (method.ReturnType.FullName != "System.Void")
{
    var varreturnValue = new VariableDefinition(method.ReturnType);
    method.Body.Variables.Add(varreturnValue);
    var lastreturn = il.Create(OpCodes.Ldloc_S, varreturnValue);
    il.Append(new[] {
        il.Create(OpCodes.Callvirt, module.Import(typeof(MethodExecutionEventArgs).GetMethod("get_ReturnValue", new Type[] { }))),
        il.Create(OpCodes.Unbox_Any, method.ReturnType), 
        il.Create(OpCodes.Stloc_S, varreturnValue),
        il.Create(OpCodes.Br_S, lastreturn), 
        lastreturn
    });
}

method.Body.Instructions.Add(ret);
#endregion
method.Body.ExceptionHandlers.Add(
    new ExceptionHandler(ExceptionHandlerType.Catch)
    {
        HandlerEnd = endTry,
        HandlerStart = tryEnd,
        TryEnd = tryEnd,
        TryStart = trySatrt,
        CatchType = module.Import(typeof(Exception))
    });

简单的if-else try-catch嵌套,这个不会报错:

源代码:

var il = method.Body.GetILProcessor();
var module = method.Module;

method.Body.Instructions.Clear();
method.Body.ExceptionHandlers.Clear();
method.Body.Variables.Clear();

#region 定义变量并且赋值
           
var varexception = new VariableDefinition(module.Import(typeof(System.Exception)));
method.Body.Variables.Add(varexception);

#endregion


#region Try

var varflag = new VariableDefinition(module.Import(typeof(bool)));
method.Body.Variables.Add(varflag);
var varflag1 = new VariableDefinition(module.Import(typeof(bool)));
method.Body.Variables.Add(varflag1);
var endIf = il.Create(OpCodes.Nop);
il.Append(new[] { 
    il.Create(OpCodes.Nop),
    il.Create(OpCodes.Ldc_I4_1),
    il.Create(OpCodes.Stloc_1),
    il.Create(OpCodes.Ldloc_1),
    il.Create(OpCodes.Ldc_I4_1),
    il.Create(OpCodes.Ceq),
    il.Create(OpCodes.Stloc_2),
    il.Create(OpCodes.Ldloc_2),
    il.Create(OpCodes.Brtrue_S, endIf),
    il.Create(OpCodes.Nop)
});

var endTry = il.Create(OpCodes.Nop);
var trySatrt = il.Create(OpCodes.Nop);
var tryEnd = il.Create(OpCodes.Stloc_S, varexception);

il.Append(new[] { 
    trySatrt,
    il.Create(OpCodes.Nop),
    il.Create(OpCodes.Leave_S, endTry), 
    tryEnd, 
    il.Create(OpCodes.Leave_S, endTry)
});

#endregion

#region End
il.Append(endTry);
il.Append(il.Create(OpCodes.Nop));
il.Append(endIf);

var ret = il.Create(OpCodes.Ret);
il.Append(il.Create(OpCodes.Br_S, ret));

method.Body.Instructions.Add(ret);
#endregion
method.Body.ExceptionHandlers.Add(
    new ExceptionHandler(ExceptionHandlerType.Catch)
    {
        HandlerEnd = endTry,
        HandlerStart = tryEnd,
        TryEnd = tryEnd,
        TryStart = trySatrt,
        CatchType = module.Import(typeof(Exception))
    });

IL代码:

.method private hidebysig static 
    void aa1 () cil managed 
{
    .custom instance void AOPTest.MethodInterceptAttribute::.ctor() = (
        01 00 00 00
    )
    // Method begins at RVA 0x222c
    // Code size 26 (0x1a)
    .maxstack 2
    .locals (
        [0] class [mscorlib]System.Exception,
        [1] bool,
        [2] bool
    )

    IL_0000: nop
    IL_0001: ldc.i4.1
    IL_0002: stloc.1
    IL_0003: ldloc.1
    IL_0004: ldc.i4.1
    IL_0005: ceq
    IL_0007: stloc.2
    IL_0008: ldloc.2
    IL_0009: brtrue.s IL_0016

    IL_000b: nop
    .try
    {
        IL_000c: nop
        IL_000d: nop
        IL_000e: leave.s IL_0014
    } // end .try
    catch [mscorlib]System.Exception
    {
        IL_0010: stloc.s 0
        IL_0012: leave.s IL_0014
    } // end handler

    IL_0014: nop
    IL_0015: nop

    IL_0016: nop
    IL_0017: br.s IL_0019

    IL_0019: ret
} // end of method Program::aa1
刑ˇ天的主页 刑ˇ天 | 初学一级 | 园豆:87
提问于:2014-10-30 17:18
< >
分享
所有回答(2)
0

外部定义

CaiYongji | 园豆:1267 (小虾三级) | 2014-10-31 18:10

不知道什么意思?

支持(0) 反对(0) 刑ˇ天 | 园豆:87 (初学一级) | 2014-11-01 22:07
0

哥,你能报错到这里来,说明你无敌了.

【秦时明月】 | 园豆:803 (小虾三级) | 2014-11-01 14:00

哥,我已经卡这里动不了了

支持(0) 反对(0) 刑ˇ天 | 园豆:87 (初学一级) | 2014-11-06 10:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册