Beispiel #1
0
        private static Instruction GetMethodRefOp2(ILProcessorEx ILprocessorEx, MethodReference callMethodRef, string methodOpName)
        {
            MethodReference MethodDefOp = null;
            var             typeDef     = callMethodRef.DeclaringType.Resolve();

            foreach (var method in typeDef.Methods)
            {
                if (method.HasThis)
                {
                    continue;
                }
                if (method.Name != methodOpName)
                {
                    continue;
                }
                if (method.ReturnType.FullName != "System.Void") //
                {
                    continue;
                }
                //if (callMethodRef.ReturnType.FullName) //
                //    continue;
                if ((method.Parameters.Count - 1) != callMethodRef.Parameters.Count)
                {
                    continue;
                }

                var match = true;
                for (int i = 0; i < callMethodRef.Parameters.Count; i++)
                {
                    if (method.Parameters[i].ParameterType.IsByReference != callMethodRef.Parameters[i].ParameterType.IsByReference)
                    {
                        match = false;
                        continue;
                    }
                }

                if (match != true)
                {
                    continue;
                }

                //if (method.Parameters[0].ParameterType.IsByReference == false &&
                //    method.Parameters[1].ParameterType.IsByReference == true)
                MethodDefOp = method;
                break;
            }

            if (MethodDefOp == null)
            {
                //method not found
                return(null);
            }

            var methodRefOp       = callMethodRef.DeclaringType.Module.ImportReference(MethodDefOp);
            var op_outInstruction = ILprocessorEx.Create(OpCodes.Call, methodRefOp);

            return(op_outInstruction);
        }
Beispiel #2
0
        private static Instruction Stloc2Ldloca(ILProcessorEx ILprocessorEx, Instruction StlocInstruction, out int n)
        {
            if (StlocInstruction.OpCode.Code == Code.Stloc)
            {
                var varDef = StlocInstruction.Operand as VariableDefinition;
                n = varDef.Index;
                return(ILprocessorEx.Create(OpCodes.Ldloca, ILprocessorEx.Method.Body.Variables[n]));
            }
            else if (StlocInstruction.OpCode.Code == Code.Stloc_S)
            {
                var varDef = StlocInstruction.Operand as VariableDefinition;
                n = varDef.Index;
            }
            else if (StlocInstruction.OpCode.Code == Code.Stloc_0)
            {
                n = 0;
            }
            else if (StlocInstruction.OpCode.Code == Code.Stloc_1)
            {
                n = 1;
            }
            else if (StlocInstruction.OpCode.Code == Code.Stloc_2)
            {
                n = 2;
            }
            else if (StlocInstruction.OpCode.Code == Code.Stloc_3)
            {
                n = 3;
            }
            else
            {
                throw new InvalidOperationException();
            }

            return(ILprocessorEx.Create(OpCodes.Ldloca_S, ILprocessorEx.Method.Body.Variables[n]));
        }
Beispiel #3
0
        private static Instruction Ldarg2Ldarga(ILProcessorEx ILprocessorEx, Instruction LdargInstruction, out int n)
        {
            if (LdargInstruction.OpCode.Code == Code.Ldarg)
            {
                var varDef = LdargInstruction.Operand as VariableDefinition;
                n = varDef.Index;
                return(ILprocessorEx.Create(OpCodes.Ldarga, ILprocessorEx.Method.Parameters[n]));
            }
            else if (LdargInstruction.OpCode.Code == Code.Ldarg_S)
            {
                var varDef = LdargInstruction.Operand as VariableDefinition;
                n = varDef.Index;
            }
            else if (LdargInstruction.OpCode.Code == Code.Ldarg_0)
            {
                n = 0;
            }
            else if (LdargInstruction.OpCode.Code == Code.Ldarg_1)
            {
                n = 1;
            }
            else if (LdargInstruction.OpCode.Code == Code.Ldarg_2)
            {
                n = 2;
            }
            else if (LdargInstruction.OpCode.Code == Code.Ldarg_3)
            {
                n = 3;
            }
            else
            {
                throw new InvalidOperationException();
            }

            return(ILprocessorEx.Create(OpCodes.Ldarga_S, ILprocessorEx.Method.Parameters[n]));
        }