Exemple #1
0
        private ClimbDelegate <T> CreateReferenceDelegate <T>(Type runtimeType)
        {
            var processor = Expression.Parameter(typeof(object), "processor");
            var value     = Expression.Parameter(typeof(T), "value");

            Expression owner = value.Convert(runtimeType);

            Expression castedProcessor = processor.Convert(_processorType);

            IEnumerable <IStateMember> members =
                _stateMemberProvider.Provide(runtimeType);

            List <Expression>          expressions         = new List <Expression>();
            List <ParameterExpression> descriptorVariables = new List <ParameterExpression>();

            foreach (IStateMember member in members)
            {
                DescriptorWriter writer = new DescriptorWriter(_climbStore);

                DescriptorVariable descriptor =
                    writer.GetDescriptor(castedProcessor, owner, member, member.MemberType);

                Expression callProcessor =
                    _mutator.GetExpression(castedProcessor, owner, member, descriptor.Reference);

                descriptorVariables.Add(descriptor.Reference);
                expressions.Add(descriptor.Declaration);
                expressions.Add(callProcessor);
            }

            BlockExpression climbBody =
                Expression.Block(descriptorVariables, expressions);

            Expression <ClimbDelegate <T> > lambda =
                Expression.Lambda <ClimbDelegate <T> >(climbBody,
                                                       "Climb_" + runtimeType.Name,
                                                       new[] { processor, value });

            ClimbDelegate <T> result = _compiler.Compile(lambda);

            return(result);
        }
        public RouteDelegate GetRouteDelegate(IStateMember member, Type runtimeMemberType)
        {
            ParameterExpression processor          = Expression.Parameter(typeof(object), "processor");
            ParameterExpression owner              = Expression.Parameter(typeof(object), "owner");
            ParameterExpression skipSpecialMethods = Expression.Parameter(typeof(bool), "skipSpecialMethods");

            var castedProcessor =
                processor.Convert(_processorType);

            DescriptorWriter descriptorWriter = new DescriptorWriter(_climbStore);

            DescriptorVariable descriptor =
                descriptorWriter.GetDescriptor(processor, owner, member, runtimeMemberType);

            MethodInfo methodToCall =
                _methodMapper.GetMethod(_processorType, member, runtimeMemberType, true);

            MethodCallExpression callProcess =
                Expression.Call(castedProcessor, methodToCall, descriptor.Reference);

            Expression callProcessWithSpecialMethods =
                _specialMutator.Mutate(callProcess, castedProcessor, owner, member, descriptor.Reference);

            BlockExpression body =
                Expression.Block(new[] { descriptor.Reference },
                                 descriptor.Declaration,
                                 Expression.Condition(skipSpecialMethods,
                                                      callProcess,
                                                      callProcessWithSpecialMethods));

            Expression <RouteDelegate> lambda =
                Expression.Lambda <RouteDelegate>(body,
                                                  "Route_" + runtimeMemberType.Name,
                                                  new[] { processor, owner, skipSpecialMethods });

            return(_compiler.Compile(lambda));
        }