Hello...I am trying to use somekind of aop to profile my methods , I am using
the sample in mono website...but I am getting 
Common Language Runtime detected an invalid program.
I searched for documentation for the methods and I couldnt find so...It
would be great if someone could help me fix my code:

here is the interception :

     CilWorker worker = method.Body.CilWorker;


                            MethodInfo StartMethod =
typeof(LoggerClass).GetMethod("Start");
                            MethodInfo FinishMethod =
typeof(LoggerClass).GetMethod("Finish");
                            ConstructorInfo ConstructorInfo =
typeof(LoggerClass).GetConstructors()[0];

                            MethodReference ConstructorMethod =
assembly.MainModule.Import(ConstructorInfo);
                            MethodReference StartMethodReference =
assembly.MainModule.Import(StartMethod);
                            MethodReference FinishMethodReference =
assembly.MainModule.Import(FinishMethod);


                            Instruction ConstructorInstruction =
worker.Create(OpCodes.Newobj, ConstructorMethod);
                            Instruction StartInstruction =
worker.Create(OpCodes.Callvirt, StartMethodReference);
                            Instruction FinishInstruction =
worker.Create(OpCodes.Callvirt, FinishMethodReference);


                            //Getting the first instruction of the current
method
                            Instruction FirstInstruction =
method.Body.Instructions[0];

                            //Inserts the insertSentence instruction before
the first 
                            //instruction
                           
method.Body.CilWorker.InsertBefore(FirstInstruction,
ConstructorInstruction);
                           
method.Body.CilWorker.InsertAfter(ConstructorInstruction, StartInstruction);

                            Instruction LastInstruction =
method.Body.Instructions[method.Body.Instructions.Count - 1];

                           
method.Body.CilWorker.InsertBefore(LastInstruction, FinishInstruction);



here is my logging class :

public class LoggerClass
    {
        private Stopwatch _CurrentStopWatch;

        public LoggerClass()
        {
            Console.WriteLine("Constructor Entered");
            _CurrentStopWatch = new Stopwatch();
        }

        public void Start()
        {
            Console.WriteLine("Start Entered");
            _CurrentStopWatch = new Stopwatch();
            _CurrentStopWatch.Start();
        }

        public void Finish()
        {
            Console.WriteLine("Finish Entered");
            _CurrentStopWatch.Stop();
            Console.WriteLine("Elapsed milliseconds are :" +
_CurrentStopWatch.ElapsedMilliseconds.ToString());
        }
    }



--
View this message in context: 
http://mono.1490590.n4.nabble.com/Common-Language-Runtime-detected-an-invalid-program-when-injecting-code-for-aop-tp3419478p3419478.html
Sent from the Mono - Cecil mailing list archive at Nabble.com.

-- 
--
mono-cecil

Reply via email to