Exemplo n.º 1
0
        private void ComposeInstanceMethodInstructions(TypeDefinition type, MethodDefinition method, VariableDefinition delegateVariable, VariableDefinition boolVariable, VariableDefinition methodKeyVariable, Instruction firstInstruction, ParameterDefinition[] parameters, InstructionComposer composer, MethodReference getInstanceMethod, ModuleDefinition md)
        {
            if (method.IsConstructor)
            {
                var arrayListConstructor          = typeof(ArrayList).GetConstructor(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new Type[] { }, null);
                var arrayListConstructorReference = md.ImportReference(arrayListConstructor);
                composer
                .LoadArg_0()
                .NewObject(arrayListConstructorReference)
                .Store(ctorParameters);

                foreach (var parameter in method.Parameters)
                {
                    composer.LoadArg_0()
                    .Load(ctorParameters)
                    .LoadArg(parameter);
                    if (parameter.ParameterType.IsValueType)
                    {
                        composer.Box(md.ImportReference(parameter.ParameterType));
                    }
                    composer
                    .InstanceCall(new Method
                    {
                        ParentType         = typeof(ArrayList),
                        MethodName         = "Add",
                        ParameterSignature = new Type[] { typeof(Object) }
                    })
                    .Pop();
                }
            }
            composer
            .Load(method.Name)
            .LoadArray(parameters.Length, typeof(string), parameters.Select(x => x.ParameterType.FullName).ToArray())
            .StaticCall(new Method
            {
                ParentType         = typeof(Core.Helper),
                MethodName         = nameof(Core.Helper.GetMethodKey),
                ParameterSignature = new[] { typeof(string), typeof(string[]) }
            }).
            Store(methodKeyVariable)
            .LoadArg_0()
            .Load(methodKeyVariable)
            .InstanceCall(getInstanceMethod)
            .Store(delegateVariable)
            .Load(delegateVariable)
            .IsNotNull()
            .Store(boolVariable)
            .Load(boolVariable)
            .MoveToWhenFalse(firstInstruction)
            .NoOperation()
            .Load(delegateVariable)
            .LoadArray(parameters, true)
            .InstanceCall(new Method
            {
                ParentType         = typeof(Delegate),
                MethodName         = nameof(Delegate.DynamicInvoke),
                ParameterSignature = new[] { typeof(object[]) }
            });
        }