Hi,

I must be missing something stupid, but I'm adding a method and some
fields to a type, but then I get a stack underflow verification error
with PEVerify:

My emitted IL looks like:

.method private hidebysig static void _SetMethodFields() cil managed
{
    .maxstack 1
    L_0001: ldnull
    L_0002: stfld class [mscorlib]System.Reflection.MethodBase
NSight.Workstation.Applications.Common.com.ipreo.wsirm.securityservice.SecurityService::ctor_0
  L_005b: ret


}

.field private static class [mscorlib]System.Reflection.MethodBase
ctor_0

 ...I don't see why it would cause a stack underflow...? It's static,
so I should null to load any target for the stfld, right? And I've
compiled a similar program and it looks the same.

Here is my code that's adding the method:

            var methodField = new FieldDefinition("{0}
_{1}".FormatWith(method.Name.Replace(".", string.Empty),
method.DeclaringType.Methods.Where(m => m.Name ==
method.Name).ToList().IndexOf(method)),
Mono.Cecil.FieldAttributes.Private |
Mono.Cecil.FieldAttributes.Static, _methodBaseReference);
            method.DeclaringType.Fields.Add(methodField);

            const string setMethodFieldsMethodName =
"_SetMethodFields";
            var setMethodFields =
method.DeclaringType.Methods.FirstOrDefault(m => m.Name ==
setMethodFieldsMethodName);
            ILProcessor setMethodFieldsProcessor;
            if (setMethodFields == null)
            {
                setMethodFields = new
MethodDefinition(setMethodFieldsMethodName,
Mono.Cecil.MethodAttributes.Private |
Mono.Cecil.MethodAttributes.Static|
Mono.Cecil.MethodAttributes.HideBySig, _voidReference);
                method.DeclaringType.Methods.Add(setMethodFields);
                setMethodFieldsProcessor =
setMethodFields.Body.GetILProcessor();
 
setMethodFieldsProcessor.Append(setMethodFieldsProcessor.Create(OpCodes.Ret));
            }
            else
            {
                setMethodFieldsProcessor =
setMethodFields.Body.GetILProcessor();
            }

            var retInstruction =
setMethodFields.Body.Instructions.Single(i => i.OpCode.Code ==
Code.Ret);

            setMethodFieldsProcessor.InsertBefore(retInstruction,
setMethodFieldsProcessor.Create(OpCodes.Ldnull));
            setMethodFieldsProcessor.InsertBefore(retInstruction,
setMethodFieldsProcessor.Create(OpCodes.Stfld, methodField));

Thanks for any help in advance.

-- 
--
mono-cecil

Reply via email to