public void Visit(IBehaviorDefinition method, CustomAttribute attribute) { ILProcessor processor = method.Body.GetILProcessor(); string description = attribute.Properties.Where(argument => (argument.Name == "Description")).First().Argument.Value as string; var listType = typeof(List<object>); var listVariable = new VariableDefinition("argumentz", method.Module.Import(listType)); method.Body.Variables.Add(listVariable); var instructions = new List<Instruction>(); instructions.Add(processor.Create(OpCodes.Newobj, method.Module.Import(listType.GetConstructor(new Type[] { })))); instructions.Add(processor.Create(OpCodes.Stloc, listVariable)); int parameterIndex = 1; foreach (var parameter in method.Parameters) { instructions.Add(processor.Create(OpCodes.Ldloc, listVariable)); instructions.Add(processor.Create(OpCodes.Ldarg, parameterIndex)); if (parameter.ParameterType.IsPrimitive || parameter.ParameterType.IsValueType) instructions.Add(processor.Create(OpCodes.Box, parameter.ParameterType)); instructions.Add(processor.Create(OpCodes.Callvirt, method.Module.Import(listType.GetMethod("Add", new[] { typeof(object) })))); ++parameterIndex; } instructions.Add(processor.Create(OpCodes.Newobj, method.Module.Import(broadcastType.GetConstructor(new Type[] {})))); instructions.Add(processor.Create(OpCodes.Ldloc, listVariable)); instructions.Add(processor.Create(OpCodes.Ldstr, description)); instructions.Add(processor.Create(OpCodes.Callvirt, method.Module.Import(broadcastType.GetMethod("Run", new[] {listType, typeof(string)})))); new InsertionAtStart(processor, method).Run(instructions); }
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute) { ILProcessor processor = behaviorDefinition.Body.GetILProcessor(); var pingerDefinition = new VariableDefinition("pinger", behaviorDefinition.Module.Import(breakpointType)); behaviorDefinition.Body.Variables.Add(pingerDefinition); var sequencedInstructions = behaviorDefinition.Body.Instructions.Where(instruction => instruction.SequencePoint != null).ToList(); Console.Out.WriteLine(sequencedInstructions.Count); // var extractedInstructions = Specific(sequencedInstructions, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 20, 21); var extractedInstructions = Filtered(sequencedInstructions); foreach (var instruction in extractedInstructions) { Type intType = typeof(int); var breakPointInstructions = new List<Instruction> { processor.Create(OpCodes.Newobj, behaviorDefinition.Module.Import( breakpointType.GetConstructor(new Type[]{}))), processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.StartLine), processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.StartColumn), processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.EndLine), processor.Create(OpCodes.Ldc_I4, instruction.SequencePoint.EndColumn), processor.Create(OpCodes.Ldstr, instruction.SequencePoint.Document.Url), processor.Create(OpCodes.Call, behaviorDefinition.Module.Import(breakpointType.GetMethod("Activate", new[] {intType, intType, intType, intType, typeof(string)}))) }; breakPointInstructions.ForEach(bpr => processor.InsertBefore(instruction, bpr)); foreach (var i in behaviorDefinition.Body.Instructions) { Console.Out.WriteLine("{0}/[{1}]: {2} {3}", i.Offset, Sequence(i.SequencePoint), i.OpCode, i.Operand); } } }
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute) { ILProcessor processor = behaviorDefinition.Body.GetILProcessor(); var pingerDefinition = new VariableDefinition("pinger", behaviorDefinition.Module.Import(pingType)); behaviorDefinition.Body.Variables.Add(pingerDefinition); var start = new List<Instruction> { processor.Create(OpCodes.Ldstr, behaviorDefinition.Name), processor.Create(OpCodes.Newobj, behaviorDefinition.Module.Import( pingType.GetConstructor(new[]{typeof(string)}))), processor.Create(OpCodes.Stloc, pingerDefinition), processor.Create(OpCodes.Ldloc, pingerDefinition), processor.Create(OpCodes.Callvirt, behaviorDefinition.Module.Import(pingType.GetMethod("Start", new Type[] {}))), }; var end = new List<Instruction> { processor.Create(OpCodes.Ldloc, pingerDefinition), processor.Create(OpCodes.Callvirt, behaviorDefinition.Module.Import(pingType.GetMethod("End", new Type[] {}))) }; new InsertionAtStart(processor, behaviorDefinition).Run(start); new InsertionAtEnd(processor, behaviorDefinition).Run(end); foreach (var instruction in behaviorDefinition.Body.Instructions) { Console.Out.WriteLine("{0}/[{1}]: {2} {3}", instruction.Offset, Sequence(instruction.SequencePoint), instruction.OpCode, instruction.Operand); } }
public void Visit(IBehaviorDefinition method, CustomAttribute attribute) { ILProcessor processor = method.Body.GetILProcessor(); string description = attribute.Properties.Where(argument => (argument.Name == "Description")).First().Argument.Value as string; Instruction exitInstruction = processor.Create(OpCodes.Callvirt, method.Module.Import(broadcastType.GetMethod("Run", new[] {typeof(object), typeof(string)}))); var returnValue = new VariableDefinition("retVal", method.Module.Import(typeof(object))); var enclosingObject = new VariableDefinition("enclosing", method.Module.Import(typeof(object))); method.Body.Variables.Add(enclosingObject); method.Body.Variables.Add(returnValue); Instruction store = processor.Create(OpCodes.Stloc, returnValue); Instruction reload = processor.Create(OpCodes.Ldloc, returnValue); var instructions = new List<Instruction>(); if (!ReturnsVoid(method)) { instructions.Add(store); } instructions.Add(processor.Create(OpCodes.Newobj, method.Module.Import(broadcastType.GetConstructor(new Type[] { })))); instructions.Add(processor.Create(OpCodes.Ldarg_0)); instructions.Add(processor.Create(OpCodes.Ldstr, description)); instructions.Add(exitInstruction); if (!ReturnsVoid(method)) { instructions.Add(reload); } new InsertionAtEnd(processor, method).Run(instructions); }
public void Visit(IBehaviorDefinition behavior, CustomAttribute attribute) { ILProcessor processor = behavior.Body.GetILProcessor(); var instructions = new List<Instruction>(); Type stackTraceType = typeof (StackTrace); instructions.Add(processor.Create(OpCodes.Newobj, behavior.Module.Import( stackTraceBroadcastType.GetConstructor(new Type[] {})))); instructions.Add(processor.Create(OpCodes.Newobj, behavior.Module.Import(stackTraceType.GetConstructor(new Type[] {})))); instructions.Add(processor.Create(OpCodes.Callvirt, behavior.Module.Import(stackTraceBroadcastType.GetMethod("Run", new[] { stackTraceType })))); new InsertionAtEnd(processor, behavior).Run(instructions); }
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute) { var first = attribute.Properties.Where(argument => (argument.Name == "Target")).First(); var target = ((PublishTarget) first.Argument.Value); switch (target) { case PublishTarget.Return: new PublishReturnValueWeave(returnValueHandler).Visit(behaviorDefinition, attribute); break; case PublishTarget.Self: new PublishSelfWeave(selfHandler).Visit(behaviorDefinition, attribute); break; case PublishTarget.Arguments: new PublishArgumentsWeave(argumentsHandler).Visit(behaviorDefinition, attribute); break; } }
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute) { var maximum = (int) attribute.Properties.Where(argument => (argument.Name == "Maximum")).First().Argument.Value; ILProcessor processor = behaviorDefinition.Body.GetILProcessor(); Type expectationType = typeof (PerformanceExpectation); var timerDefinition = new VariableDefinition("timer", behaviorDefinition.Module.Import(monitorType)); behaviorDefinition.Body.Variables.Add(timerDefinition); var start = new List<Instruction> { processor.Create(OpCodes.Call, behaviorDefinition.Module.Import( typeof (DateTime).GetMethod("get_Now", new Type[] {}))), processor.Create(OpCodes.Ldc_I4, maximum), processor.Create(OpCodes.Newobj, behaviorDefinition.Module.Import( expectationType.GetConstructor(new[] {typeof (int)}))), processor.Create(OpCodes.Newobj, behaviorDefinition.Module.Import( monitorType.GetConstructor(new[] {typeof (DateTime), expectationType}))), processor.Create(OpCodes.Stloc, timerDefinition) }; var end = new List<Instruction> { processor.Create(OpCodes.Ldloc, timerDefinition), processor.Create(OpCodes.Callvirt, behaviorDefinition.Module.Import(monitorType.GetMethod("End", new Type[] {}))) }; new InsertionAtStart(processor, behaviorDefinition).Run(start); new InsertionAtEnd(processor, behaviorDefinition).Run(end); }
public InsertionAtEnd(ILProcessor processor, IBehaviorDefinition definition) : base(processor, definition) { }
protected PositionalInsertion(ILProcessor processor, IBehaviorDefinition definition) { this.processor = processor; this.definition = definition; }
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute) { Console.Out.WriteLine("{0} - [{1}]", behaviorDefinition.Name, attribute.Constructor.DeclaringType.FullName); }
public void Visit(IBehaviorDefinition behaviorDefinition, CustomAttribute attribute) { }
private bool ReturnsVoid(IBehaviorDefinition method) { TypeReference voidType = method.Module.Import(typeof(void)); return method.ReturnType.FullName.Equals(voidType.FullName); }