コード例 #1
0
        private static Func <Expression, LambdaExpression> FixedLambdaFactory(Type target, string method, InjectLambdaSignature signature)
        {
            // retrieve validated factory method once
            var factory = signature.FindFactory(target, method);

            if (factory.IsStatic)
            {
                // compile factory call for performance reasons :-)
                return(Expression.Lambda <Func <Expression, LambdaExpression> >(
                           Expression.Call(factory), Expression.Parameter(typeof(Expression))).Compile());
            }

            // call actual target object, compiles every time during execution... :-|
            return(value => Expression.Lambda <Func <LambdaExpression> >(Expression.Call(value, factory)).Compile()());
        }
コード例 #2
0
        private static Func <Expression, LambdaExpression> DynamicLambdaFactory(string method, InjectLambdaSignature signature)
        {
            return(value =>
            {
                // retrieve actual target object, compiles every time and needs reflection too... :-(
                var targetObject = Expression.Lambda <Func <object> >(Expression.Convert(value, typeof(object))).Compile()();

                // retrieve actual target type at runtime, whatever it may be
                var target = targetObject.GetType();

                // actual method may provide different information
                var concreteMethod = signature.FindMatch(target, method);

                // configuration over convention, if any
                var metadata = InjectLambdaAttribute.GetCustomAttribute(concreteMethod) ?? InjectLambdaAttribute.None;

                // retrieve validated factory method
                var factory = signature.FindFactory(target, metadata.Method ?? method);

                // finally call lambda factory *uff*
                return (LambdaExpression)factory.Invoke(targetObject, null);
            });
        }