Exemplo n.º 1
0
        void SetExpressions()
        {
            var objParam   = Expression.Parameter(typeof(object), "obj");
            var getterExpr = GetterExpression.GetBody(Expression.Convert(objParam, TypeAccessor.Type));
            var getter     = Expression.Lambda <Func <object, object> >(Expression.Convert(getterExpr, typeof(object)), objParam);

            Getter = getter.Compile();

            var valueParam = Expression.Parameter(typeof(object), "value");
            var setterExpr = SetterExpression.GetBody(
                Expression.Convert(objParam, TypeAccessor.Type),
                Expression.Convert(valueParam, Type));
            var setter = Expression.Lambda <Action <object, object> >(setterExpr, objParam, valueParam);

            Setter = setter.Compile();
        }
Exemplo n.º 2
0
        void SetExpressions()
        {
            var objParam   = Expression.Parameter(typeof(object), "obj");
            var getterExpr = GetterExpression.GetBody(Expression.Convert(objParam, TypeAccessor.Type));
            var getter     = Expression.Lambda <Func <object, object?> >(Expression.Convert(getterExpr, typeof(object)), objParam);

            try
            {
                Getter = getter.Compile();
            }
            catch (Exception e)
            {
                ThrowCompileException(getter, e, true);
            }

            var valueParam = Expression.Parameter(typeof(object), "value");

            if (SetterExpression != null)
            {
                var setterExpr = SetterExpression.GetBody(
                    Expression.Convert(objParam, TypeAccessor.Type),
                    Expression.Convert(valueParam, Type));
                var setter = Expression.Lambda <Action <object, object?> >(setterExpr, objParam, valueParam);
                try
                {
                    Setter = setter.Compile();
                }
                catch (Exception e)
                {
                    ThrowCompileException(setter, e, false);
                }
            }

            void ThrowCompileException(Expression expression, Exception e, bool isGetter)
            {
                string message = String.Format("Failed to compile {0} expression for '{1}'. Expression: {2}",
                                               (isGetter ? "Getter" : "Setter"), this.Name, expression.ToString()
                                               );

                throw new InvalidOperationException(message, e);
            }
        }