public MethodCopier(MethodBase fromMethod, ILGenerator toILGenerator, LocalBuilder[] existingVariables = null) { if (fromMethod == null) { throw new ArgumentNullException("Method cannot be null"); } reader = new MethodBodyReader(fromMethod, toILGenerator); reader.DeclareVariables(existingVariables); reader.ReadInstructions(); }
public static List <ILInstruction> GetInstructions(MethodBase method) { if (method == null) { throw new Exception("method cannot be null"); } var reader = new MethodBodyReader(method, null); reader.ReadInstructions(); return(reader.ilInstructions); }
public MethodCopier(MethodBase fromMethod, DynamicMethod toDynamicMethod, LocalBuilder[] existingVariables = null) { if (fromMethod == null) { throw new Exception("method cannot be null"); } var generator = toDynamicMethod.GetILGenerator(); reader = new MethodBodyReader(fromMethod, generator); reader.DeclareVariables(existingVariables); reader.ReadInstructions(); }
// NOTE: you cannot simply "copy" ILInstructions from a method. They contain references to // local variables which must be CREATED on an ILGenerator or else they are invalid when you // want to use the ILInstruction. If you are really clever, you can supply a dummy generator // and edit out all labels during the processing but that might be more trickier than you think // // In order to copy together a bunch of method parts within a transpiler, you have to pass in // your current generator that builds your new method // // You will end up with the sum of all declared local variables of all methods you run // GetInstructions on or use a dummy generator but edit out the invalid labels from the codes // you copy // public static List <ILInstruction> GetInstructions(ILGenerator generator, MethodBase method) { if (method == null) { throw new ArgumentNullException("Method cannot be null"); } var reader = new MethodBodyReader(method, generator); reader.DeclareVariables(null); reader.ReadInstructions(); return(reader.ilInstructions); }