Hi! What does peverify say when you run it on your modified program?
Thanks, Jb On Thu, Jan 8, 2015 at 4:09 PM, Reh Gina <[email protected]> wrote: > I try to add a try finally block to my methods but it does not work > chrchrchr. Try catch works, but with try finally the unit test cannot load > the dll because of 'Common Runtime Language detected invalid program' > What am I doing wrong, PLEASE HELP > > //// Start code ////// > > med.Body.SimplifyMacros(); > > ILProcessor ilProcessor = med.Body.GetILProcessor(); > > var firstInstruction = FindFirstInstructionSkipCtor(med); > > var fTraceVar = new VariableDefinition("FTraceVar", > mod.Import(typeof(syngo.Common.Diagnostics.Tracing.FTrace))); > med.Body.Variables.Add(fTraceVar); > > /////////////// Start of try block > Instruction loadMethodNameInstr = Instruction.Create(OpCodes.Ldstr, > med.Name); > ilProcessor.InsertAfter(firstInstruction, loadMethodNameInstr); > > Instruction loadDomainInstruction = Instruction.Create(OpCodes.Ldsfld, > fTraceDomainField); > ilProcessor.InsertBefore(loadMethodNameInstr, loadDomainInstruction); > > var constructor = > typeof(syngo.Common.Diagnostics.Tracing.FTrace).GetConstructor( > new[] > { > typeof(syngo.Common.Diagnostics.Tracing.FTraceDomain), > typeof(string) > }); > var constructorReference = mod.Import(constructor); > Instruction newConstructorInstruction = > Instruction.Create(OpCodes.Newobj, constructorReference); > ilProcessor.InsertAfter(loadMethodNameInstr, > newConstructorInstruction); // create new instance of person > > Instruction popFTraceVariableInstruction = > Instruction.Create(OpCodes.Stloc, fTraceVar); > //Pop a value from stack and store into local variable at index. > ilProcessor.InsertAfter(newConstructorInstruction, > popFTraceVariableInstruction); > > var endFinally = Instruction.Create(OpCodes.Endfinally); > var leave = Instruction.Create(OpCodes.Leave, endFinally); > > > > //////// Start Finally block > Instruction loadFTraceVarInstruction = > Instruction.Create(OpCodes.Ldloca, fTraceVar); > ilProcessor.InsertAfter(med.Body.Instructions.Last(), > loadFTraceVarInstruction); //Loads local variable onto stack > //ilProcessor.InsertBefore(lastInstruction, loadFTraceVarInstruction); > //Loads local variable onto stack > > Instruction callDisposeInstruction = Instruction.Create(OpCodes.Call, > > mod.Import(typeof(syngo.Common.Diagnostics.Tracing.FTrace).GetMethod("Dispose"))); > ilProcessor.InsertAfter(loadFTraceVarInstruction, > callDisposeInstruction); // calls dispose > > > ilProcessor.InsertAfter(callDisposeInstruction, leave); > ilProcessor.InsertAfter(leave, endFinally); > > > ///////// End finally block > > > var handler = new ExceptionHandler(ExceptionHandlerType.Finally) > { > TryStart = med.Body.Instructions.First(), > TryEnd = callDisposeInstruction, > HandlerStart = callDisposeInstruction, > HandlerEnd = endFinally, > //CatchType = mod.Import(typeof(Exception)), > }; > > > med.Body.ExceptionHandlers.Add(handler); > med.Body.InitLocals = true; > med.Body.OptimizeMacros(); > > -- > -- > -- > mono-cecil > --- > You received this message because you are subscribed to the Google Groups > "mono-cecil" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. -- -- -- mono-cecil --- You received this message because you are subscribed to the Google Groups "mono-cecil" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
