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));
        }
        public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating)
        {
            var defaultAttribute = attribute as IDefaultProcessEncapsulatingActionAttribute;

            var funcProcessor = GetMethodInfo(() => defaultAttribute.Process(null));
            if(processor != null)
                encapsulating.Invoke(funcProcessor, processor);
            else
                encapsulating.Invoke(funcProcessor);
        }
Exemplo n.º 3
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));
            }
        }
        public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating)
        {
            var defaultAttribute = attribute as IDefaultProcessEncapsulatingActionAttribute;

            var funcProcessor = GetMethodInfo(() => defaultAttribute.Process(null));

            if (processor != null)
            {
                encapsulating.Invoke(funcProcessor, processor);
            }
            else
            {
                encapsulating.Invoke(funcProcessor);
            }
        }
Exemplo n.º 5
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.º 6
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.º 7
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.º 8
0
        private void GeneratePostProcessors(MethodBodyContext body, ICustomAttributeProvider methodInfo, GeneratedVariable serviceLocator)
        {
            var attributes = methodInfo.GetCustomAttributes(typeof(IPostProcessingAttribute), true);

            for (int i = 0; i < attributes.Length; i++)
            {
                var attribute     = attributes[i];
                var postProcessor = body.CreateVariable <IPostProcessingAttribute>();
                if (useServiceLocator)
                {
                    postProcessor.AssignFrom(() => serviceLocator.Invoke(typeof(Microsoft.Practices.ServiceLocation.IServiceLocator).GetMethod("GetInstance", new Type[0]).MakeGenericMethod(attribute.GetType())));
                }
                else
                {
                    postProcessor.AssignFrom(body.Instantiate(attribute.GetType()));
                }

                postProcessor.Invoke <IDefaultPostProcessingAttribute>(processor => processor.Process());
            }
        }
Exemplo n.º 9
0
 public void Intercept(GeneratedVariable postProcessor)
 {
     postProcessor.Invoke <IDefaultPostProcessingAttribute>(processor => processor.Process());
 }
 public void Intercept(GeneratedVariable preProcessor)
 {
     preProcessor.Invoke<IDefaultPreProcessingAttribute>(processor => processor.Process());
 }