Example #1
0
        public Expression <FunctionComponent> BuildRendererExpression()
        {
            var context = new ComponentBuilderContext(this);
            var element = null as ElementBuilder;

            try
            {
                // build element to be rendered
                element = Render(context);
            }
            catch (Exception e)
            {
                context.OnException(e);
            }

            context.ThrowExceptions();

            var body = new List <Expression>();

            // declare and initialize variables step
            var variables = context.Variables.OrderBy(x => x.Key).Select(x => x.Value).ToArray();

            foreach (var variable in variables)
            {
                body.Add(Expression.Assign(variable.Name, variable.Value));
            }

            // return rendered element step
            body.Add(element?.GetValue(context) ?? Expression.Constant(null, typeof(ofElement)));

            return(Expression.Lambda <FunctionComponent>(Expression.Block(variables.Select(v => v.Name), body), context.Node));
        }