Esempio n. 1
0
        private string GetInstanceLogMethodName(MethodReferenceInfo methodReferenceInfo)
        {
            //TODO chain inner types in name
            var typeName = methodReferenceInfo.DeclaringType.Name;

            if (methodReferenceInfo.IsPropertyAccessor())
            {
                return("get_" + typeName + methodReferenceInfo.Name.Substring(4));
            }
            else
            {
                return(typeName + methodReferenceInfo.Name);
            }
        }
Esempio n. 2
0
        private void ChangeStaticLogCallWithoutParameter(Instruction oldInstruction)
        {
            var instructions = new List <Instruction>();

            var methodReference     = (MethodReference)oldInstruction.Operand;
            var methodReferenceInfo = new MethodReferenceInfo(methodReference);

            instructions.Add(Instruction.Create(OpCodes.Ldsfld, _loggerProvider.StaticLogger));

            if (!methodReferenceInfo.IsPropertyAccessor())
            {
                instructions.AddRange(LoadMethodNameOnStack());
            }

            instructions.Add(Instruction.Create(OpCodes.Callvirt, _methodReferenceProvider.GetInstanceLogMethod(methodReferenceInfo)));

            _body.Replace(oldInstruction, instructions);
        }
Esempio n. 3
0
        ///The way how we solve this is a bit lame, but fairly simple. We store all parameters into local variables
        /// then call the instance log method reading the parameters from these variables.
        /// A better solution would be to figure out where the call really begins (where is the bottom of the stack)
        /// and insert the instance ref there plus change the call instraction
        private void ChangeStaticLogCallWithParameter(Instruction oldInstruction)
        {
            var instructions        = new List <Instruction>();
            var methodReference     = (MethodReference)oldInstruction.Operand;
            var methodReferenceInfo = new MethodReferenceInfo(methodReference);

            if (methodReferenceInfo.IsPropertyAccessor() && methodReferenceInfo.IsSetter)
            {
                throw new Exception("Rewriting static property setters is not supported.");
            }

            var parameters = methodReference.Parameters;

            //create variables to store parameters and push values into them
            var variables = new VariableDefinition[parameters.Count];

            for (int idx = 0; idx < parameters.Count; idx++)
            {
                variables[idx] = GetVariableDefinitionForType(parameters[idx].ParameterType, methodReference, _methodDefinition);
                _body.Variables.Add(variables[idx]);
            }

            //store in reverse order
            for (int idx = parameters.Count - 1; idx >= 0; idx--)
            {
                instructions.Add(Instruction.Create(OpCodes.Stloc, variables[idx]));
            }

            //build-up instance call
            instructions.Add(Instruction.Create(OpCodes.Ldsfld, _loggerProvider.StaticLogger));
            instructions.AddRange(LoadMethodNameOnStack());

            for (int idx = 0; idx < parameters.Count; idx++)
            {
                instructions.Add(Instruction.Create(OpCodes.Ldloc, variables[idx]));
            }

            instructions.Add(Instruction.Create(OpCodes.Callvirt, _methodReferenceProvider.GetInstanceLogMethod(methodReferenceInfo, parameters)));

            _body.Replace(oldInstruction, instructions);
        }
Esempio n. 4
0
        public MethodReference GetInstanceLogMethod(MethodReferenceInfo methodReferenceInfo, IEnumerable <ParameterDefinition> parameters = null)
        {
            parameters = parameters ?? new ParameterDefinition[0];

            var logMethod = new MethodReference(GetInstanceLogMethodName(methodReferenceInfo), methodReferenceInfo.ReturnType, _typeReferenceProvider.LogAdapterReference);

            logMethod.HasThis = true; //instance method

            //check if accessor
            if (!methodReferenceInfo.IsPropertyAccessor())
            {
                logMethod.Parameters.Add(new ParameterDefinition(_moduleDefinition.TypeSystem.String));
            }

            foreach (var parameter in parameters)
            {
                logMethod.Parameters.Add(parameter);
            }

            //handle generics
            if (methodReferenceInfo.IsGeneric)
            {
                foreach (var genericParameter in methodReferenceInfo.GenericParameters)
                {
                    var gp = new GenericParameter(genericParameter.Name, logMethod);
                    gp.Name = genericParameter.Name;
                    logMethod.GenericParameters.Add(gp);
                }
                logMethod.CallingConvention = MethodCallingConvention.Generic;

                logMethod = new GenericInstanceMethod(logMethod);
                foreach (var genericArgument in methodReferenceInfo.GenericArguments)
                {
                    ((GenericInstanceMethod)logMethod).GenericArguments.Add(genericArgument);
                }
            }

            return(logMethod);
        }