static MethodBody CreateTestMethod(params OpCode [] opcodes) { var method = new MethodDefinition { Name = "function", }; var il = method.Body.GetILProcessor (); foreach (var opcode in opcodes) il.Emit (opcode); return method.Body; }
public void AddVariableIndex() { var object_ref = new TypeReference ("System", "Object", null, null, false); var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref); var body = new MethodBody (method); var x = new VariableDefinition ("x", object_ref); var y = new VariableDefinition ("y", object_ref); body.Variables.Add (x); body.Variables.Add (y); Assert.AreEqual (0, x.Index); Assert.AreEqual (1, y.Index); }
public void AddInstruction() { var object_ref = new TypeReference ("System", "Object", null, null, false); var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref); var body = new MethodBody (method); var il = body.GetILProcessor (); var first = il.Create (OpCodes.Nop); var second = il.Create (OpCodes.Nop); body.Instructions.Add (first); body.Instructions.Add (second); Assert.IsNull (first.Previous); Assert.AreEqual (second, first.Next); Assert.AreEqual (first, second.Previous); Assert.IsNull (second.Next); }
public static void WriteMethodBody(TextWriter writer, MethodDefinition method) { var body = method.Body; WriteVariables (writer, body); foreach (Instruction instruction in body.Instructions) { var sequence_point = instruction.SequencePoint; if (sequence_point != null) { writer.Write ('\t'); WriteSequencePoint (writer, sequence_point); writer.WriteLine (); } writer.Write ('\t'); WriteInstruction (writer, instruction); writer.WriteLine (); } WriteExceptionHandlers (writer, body); }
static Collection<ParameterDefinition> MirrorParameters(MethodDefinition method, int bound) { var parameters = new Collection<ParameterDefinition> (); if (!method.HasParameters) return parameters; var original_parameters = method.Parameters; var end = original_parameters.Count - bound; for (int i = 0; i < end; i++) parameters.Add (original_parameters [i]); return parameters; }
public static string FormatMethodBody(MethodDefinition method) { var writer = new StringWriter (); WriteMethodBody (writer, method); return writer.ToString (); }
private static bool IsVarArgCallTo (MethodDefinition method, MethodReference reference) { if (method.Parameters.Count >= reference.Parameters.Count) return false; if (reference.GetSentinelPosition () != method.Parameters.Count) return false; for (int i = 0; i < method.Parameters.Count; i++) if (!AreSame (method.Parameters [i].ParameterType, reference.Parameters [i].ParameterType)) return false; return true; }
static void AssertCode(string expected, MethodDefinition method) { Assert.IsTrue (method.HasBody); Assert.IsNotNull (method.Body); Assert.AreEqual (Normalize (expected), Normalize (Formatter.FormatMethodBody (method))); }
public void UnattachedMethodBody() { var system_void = typeof (void).ToDefinition (); var method = new MethodDefinition ("NewMethod", MethodAttributes.Assembly | MethodAttributes.Static, system_void); var body = new MethodBody (method); var il = body.GetILProcessor (); il.Emit (OpCodes.Ret); method.Body = body; Assert.AreEqual (body, method.Body); }