Beispiel #1
0
        /// <summary>
        /// Compiles an <see cref="LambdaExpression"/> to a <see cref="Delegate"/>.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The compiled expression as a <see cref="Delegate"/>.</returns>
        public Delegate CompileExpression(LambdaExpression expression)
        {
            ArgumentChecker.NotNull(expression, () => expression);

            var type = expression.GetType();
            bool isCompilableExpression = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Expression<>);
            ArgumentChecker.Assert<ArgumentException>(isCompilableExpression, "Unexpected expression type, cannot compile", "expression");

            Delegate compiledDelegate = (Delegate)type.GetMethod("Compile", new Type[0]).Invoke(expression, new object[0]);
            return compiledDelegate;
        }