Beispiel #1
0
        public Type Create(Type typeToProxy)
        {
            if (definedTypes.ContainsKey(typeToProxy))
            {
                return((Type)definedTypes[typeToProxy]);
            }
            if (!HasAopDefinitions(typeToProxy))
            {
                return(typeToProxy);
            }

            Type generatedType = generator.CreateType(type =>
            {
                type.Named(typeToProxy.Name + Guid.NewGuid());
                type.InheritFrom(typeToProxy);
                GeneratedField field = null;

                if (useServiceLocator)
                {
                    field = type.AddField <Microsoft.Practices.ServiceLocation.IServiceLocator>("serviceLocator");
                    type.AddConstructor(constructor => constructor.CreateArgument <Microsoft.Practices.ServiceLocation.IServiceLocator>().AssignTo(field));
                }

                ProxyMethods(type, typeToProxy, field);
            });

            return(generatedType);
        }
Beispiel #2
0
        public void GenerateField(IFieldGenerator generator)
        {
            for (int y = 0; y < this.matrixNumber; y++)
            {
                for (int x = 0; x < this.matrixNumber; x++)
                {
                    this.cells[y, x].Clear();
                }
            }

            Actor.ClearEnemies();
            ClearRoom();
            generator.Generate();
            Broker.Global.Publish(GeneratedField.Get());
        }
        private GeneratedField GenerateField(FieldInfo info)
        {
            GeneratedField result = new GeneratedField();

            result.info = info;

            IAccessorCreator creator = store[info.type];

            result.getter = creator.MakeGetter(info);
            result.setter = creator.MakeSetter(info);

            result.getterDeclaration = result.getter.Split(new string[] { Environment.NewLine },
                                                           StringSplitOptions.RemoveEmptyEntries)[0];
            result.setterDeclaration = result.setter.Split(new string[] { Environment.NewLine },
                                                           StringSplitOptions.RemoveEmptyEntries)[0];

            return(result);
        }
Beispiel #4
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);
                        }
                    }));
                }
            }
        }
Beispiel #5
0
        private void RecursivelyGenerateCalls(object[] attributes, int currentIndex, DelegateBodyContext lambda, MethodInfo methodInfo, GeneratedField field)
        {
            if (currentIndex >= attributes.Length)
            {
                return;
            }
            var attribute = attributes[currentIndex];

            lambda.CreateNestedLambda(nestedLambda => RecursivelyGenerateCalls(attributes, currentIndex + 1, lambda, methodInfo, field),
                                      (exitContext, exitVariable, function, callingType) =>
            {
                var encapsulating = exitContext.CreateVariable <IProcessEncapsulatingAttribute>();

                if (useServiceLocator)
                {
                    var locator = exitContext.CreateVariable <Microsoft.Practices.ServiceLocation.IServiceLocator>();
                    locator.AssignFrom(field, callingType);
                    encapsulating.AssignFrom(() => locator.Invoke(typeof(Microsoft.Practices.ServiceLocation.IServiceLocator).GetMethod("GetInstance", new Type[0]).MakeGenericMethod(attribute.GetType())));
                }
                else
                {
                    encapsulating.AssignFrom(exitContext.Instantiate(attribute.GetType()));
                }

                var func = exitContext.CreateFunc(methodInfo.ReturnType, function());

                if (methodInfo.ReturnType != typeof(void))
                {
                    new DefaultProcessEncapsulatingInterceptionStrategy().Intercept(methodInfo, attribute, func, exitVariable, encapsulating);
                }
                else
                {
                    new DefaultProcessEncapsulatingActionInterceptionStrategy().Intercept(methodInfo, attribute, func, null, encapsulating);
                }
            });
        }
Beispiel #6
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);
        }
Beispiel #7
0
 public FieldLoadAction(GeneratedMethod method, Func <FieldInfo> field, GeneratedField parent)
 {
     this.method = method;
     this.field  = field;
     this.parent = parent;
 }
Beispiel #8
0
 public FieldLoadAction(GeneratedMethod method, Func<FieldInfo> field, GeneratedField parent)
 {
     this.method = method;
     this.field = field;
     this.parent = parent;
 }