Exemplo n.º 1
0
 internal MethodCopier(MethodBase fromMethod, ILGenerator toILGenerator, LocalBuilder[] existingVariables = null)
 {
     if (fromMethod is null)
     {
         throw new ArgumentNullException(nameof(fromMethod));
     }
     reader = new MethodBodyReader(fromMethod, toILGenerator);
     reader.DeclareVariables(existingVariables);
     reader.ReadInstructions();
 }
Exemplo n.º 2
0
        /// <summary>Returns the methods unmodified list of code instructions</summary>
        /// <param name="original">The original method/constructor</param>
        /// <param name="generator">Optionally an existing generator that will be used to create all local variables and labels contained in the result (if not specified, an internal generator is used)</param>
        /// <returns>A list containing all the original <see cref="CodeInstruction"/></returns>
        ///
        public static List <CodeInstruction> GetOriginalInstructions(MethodBase original, ILGenerator generator = null)
        {
            if (generator == null)
            {
                var method = new DynamicMethodDefinition($"{original.Name}_Copy{Guid.NewGuid()}", typeof(void), new Type[0]);
                generator = method.GetILGenerator();
            }
            var reader = MethodBodyReader.GetInstructions(generator, original);

            return(reader.Select(ins => ins.GetCodeInstruction()).ToList());
        }
Exemplo n.º 3
0
        internal static List <ILInstruction> GetInstructions(ILGenerator generator, MethodBase method)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            var reader = new MethodBodyReader(method, generator);

            reader.DeclareVariables(null);
            reader.ReadInstructions();
            return(reader.ilInstructions);
        }
Exemplo n.º 4
0
 /// <summary>A low level way to read the body of a method. Used for quick searching in methods</summary>
 /// <param name="method">The original method</param>
 /// <param name="generator">An existing generator that will be used to create all local variables and labels contained in the result</param>
 /// <returns>All instructions as opcode/operand pairs</returns>
 ///
 public static IEnumerable <KeyValuePair <OpCode, object> > ReadMethodBody(MethodBase method, ILGenerator generator)
 {
     return(MethodBodyReader.GetInstructions(generator, method)
            .Select(instr => new KeyValuePair <OpCode, object>(instr.opcode, instr.operand)));
 }
Exemplo n.º 5
0
        /// <summary>Returns the methods unmodified list of code instructions</summary>
        /// <param name="original">The original method/constructor</param>
        /// <param name="generator">Optionally an existing generator that will be used to create all local variables and labels contained in the result (if not specified, an internal generator is used)</param>
        /// <returns>A list containing all the original <see cref="CodeInstruction"/></returns>
        ///
        public static List <CodeInstruction> GetOriginalInstructions(MethodBase original, ILGenerator generator = null)
        {
            var reader = MethodBodyReader.GetInstructions(generator ?? CreateILGenerator(), original);

            return(reader.Select(ins => ins.GetCodeInstruction()).ToList());
        }
Exemplo n.º 6
0
 /// <summary>Gets all instructions from a method</summary>
 /// <param name="generator">The generator (for defining labels)</param>
 /// <param name="method">The original method</param>
 /// <returns>The instructions</returns>
 ///
 public static List <ILInstruction> GetInstructions(ILGenerator generator, MethodBase method)
 {
     return(MethodBodyReader.GetInstructions(generator, method));
 }