public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating)
        {
            var defaultAttribute = attribute as IDefaultProcessEncapsulatingAttribute;

            var funcProcessor = GetMethodInfo(() => defaultAttribute.Process<object>(null), methodInfo.ReturnType);
            if(processor != null)
                variable.AssignFrom(() => encapsulating.Invoke(funcProcessor, processor));
            else
                variable.AssignFrom(() => encapsulating.Invoke(funcProcessor));
        }
Exemplo n.º 2
0
        public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating)
        {
            var defaultAttribute = attribute as IDefaultProcessEncapsulatingAttribute;

            var funcProcessor = GetMethodInfo(() => defaultAttribute.Process <object>(null), methodInfo.ReturnType);

            if (processor != null)
            {
                variable.AssignFrom(() => encapsulating.Invoke(funcProcessor, processor));
            }
            else
            {
                variable.AssignFrom(() => encapsulating.Invoke(funcProcessor));
            }
        }
Exemplo n.º 3
0
        public void Should_Be_Able_To_Add_A_Method_And_Instantiate_A_Type_And_Invoke_A_Method()
        {
            Type generatedType = generator.CreateType(type =>
            {
                type.Named("TestType8");
                type.AddMethod(method =>
                {
                    method.Named("TestMethod");
                    method.Returns(typeof(void));
                    method.CreateArgument <string>();
                    method.WithBody(body =>
                    {
                        GeneratedVariable variable    = body.CreateVariable <Processor>();
                        GeneratedVariable returnValue = body.CreateVariable <string>();

                        variable.AssignFrom(body.Instantiate <Processor>());
                        returnValue.AssignFrom(() => variable.Invoke <Processor>(processor => processor.Process(null, null)));
                        body.Return(returnValue);
                    });
                });
            });

            Assert.IsNotNull(generatedType.GetMethod("TestMethod"));
            Assert.AreEqual(1, generatedType.GetMethod("TestMethod").GetParameters().Length);
            generatedType.GetMethod("TestMethod").Invoke(Activator.CreateInstance(generatedType), new[] { "yay" });
        }
Exemplo n.º 4
0
        public void Should_Be_Able_To_Add_An_Override_Method_And_Instantiate_A_Type_And_Invoke_A_Method()
        {
            Type generatedType = generator.CreateType(type =>
            {
                type.Named("TestType7");
                type.InheritFrom <BaseType>();
                type.OverrideMethod <BaseType>(baseType => baseType.DoSomething(null), method =>
                {
                    method.WithBody(body =>
                    {
                        GeneratedVariable variable    = body.CreateVariable <Processor>();
                        GeneratedVariable returnValue = body.CreateVariable <string>();

                        variable.AssignFrom(body.Instantiate <Processor>());
                        returnValue.AssignFrom(() => variable.Invoke <Processor>(processor => processor.Process(null, null)));

                        GeneratedVariable baseValue = body.CreateVariable <string>();
                        baseValue.AssignFrom(() => body.CallBase(method.Method));
                        body.Return(baseValue);
                    });
                });
            });

            Assert.IsNotNull(generatedType.GetMethod("DoSomething"));
            Assert.AreEqual(1, generatedType.GetMethod("DoSomething").GetParameters().Length);
            Assert.AreEqual("yay", generatedType.GetMethod("DoSomething").Invoke(Activator.CreateInstance(generatedType), new[] { "yay" }));
        }
Exemplo n.º 5
0
        private GeneratedVariable GenerateEncapsulatedCalls(MethodInfo methodInfo, MethodBodyContext body, GeneratedVariable serviceLocator, GeneratedField field)
        {
            var attributes = methodInfo.GetCustomAttributes(typeof(IProcessEncapsulatingAttribute), true);

            GeneratedVariable variable = null;

            if (attributes.Length == 0)
            {
                if (methodInfo.ReturnType == typeof(void))
                {
                    body.CallBase(methodInfo);
                }
                else
                {
                    variable = body.CreateVariable(methodInfo.ReturnType);
                    variable.AssignFrom(() => body.CallBase(methodInfo));
                }

                return(variable);
            }

            var encapsulating = body.CreateVariable <IProcessEncapsulatingAttribute>();

            if (useServiceLocator)
            {
                encapsulating.AssignFrom(() => serviceLocator.Invoke(typeof(Microsoft.Practices.ServiceLocation.IServiceLocator).GetMethod("GetInstance", new Type[0]).MakeGenericMethod(attributes[0].GetType())));
            }
            else
            {
                encapsulating.AssignFrom(body.Instantiate(attributes[0].GetType()));
            }
            MethodInfo target         = null;
            var        lambdaVariable = body.CreateLambda(lambda =>
            {
                target = lambda.Target(methodInfo);

                RecursivelyGenerateCalls(attributes, 1, lambda, methodInfo, field);
            });

            var func = lambdaVariable.CreateFunc(target);

            if (methodInfo.ReturnType != typeof(void))
            {
                variable = body.CreateVariable(methodInfo.ReturnType);
                new DefaultProcessEncapsulatingInterceptionStrategy().Intercept(methodInfo, attributes[0], func, variable, encapsulating);
            }
            else
            {
                new DefaultProcessEncapsulatingActionInterceptionStrategy().Intercept(methodInfo, attributes[0], func, null, encapsulating);
            }

            return(variable);
        }
Exemplo n.º 6
0
        private void ProxyMethods(BaseTypeGenerationContext type, Type typeToProxy, GeneratedField field)
        {
            var methods = typeToProxy.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            for (int i = 0; i < methods.Length; i++)
            {
                var methodInfo = methods[i];
                if (methodInfo.IsVirtual && methodInfo.GetBaseDefinition().DeclaringType != typeof(object))
                {
                    type.OverrideMethod(methodInfo, method => method.WithBody(body =>
                    {
                        if (methodInfo.GetCustomAttributes(typeof(IAopAttribute), true).Length == 0)
                        {
                            body.CallBase(methodInfo);
                            return;
                        }

                        GeneratedVariable locator = null;

                        if (useServiceLocator)
                        {
                            locator = body.CreateVariable <Microsoft.Practices.ServiceLocation.IServiceLocator>();
                            locator.AssignFrom(field);
                        }

                        GeneratePreProcessors(body, methodInfo, locator);

                        var returnValue = GenerateEncapsulatedCalls(methodInfo, body, locator, field);

                        GeneratePostProcessors(body, methodInfo, locator);

                        if (returnValue != null)
                        {
                            body.Return(returnValue);
                        }
                    }));
                }
            }
        }