예제 #1
0
        private Expression BuildDecoratorEnumerableExpressionForNonConstantExpression(
            Delegate wrapInstanceWithDecorator, Expression expression)
        {
            // Build the query: from item in expression select wrapInstanceWithDecorator(item);
            var callExpression =
                DecoratorHelpers.Select(expression, this.registeredServiceType, wrapInstanceWithDecorator);

            if (this.Lifestyle == Lifestyle.Singleton)
            {
                Type enumerableServiceType =
                    typeof(IEnumerable <>).MakeGenericType(this.registeredServiceType);

                IEnumerable CreateCollection()
                {
                    Type     funcType            = typeof(Func <>).MakeGenericType(enumerableServiceType);
                    Delegate lambda              = Expression.Lambda(funcType, callExpression).Compile();
                    var      decoratedCollection = (IEnumerable)lambda.DynamicInvoke();
                    Array    array = ToArray(this.registeredServiceType, decoratedCollection);

                    return(DecoratorHelpers.MakeReadOnly(this.registeredServiceType, array));
                }

                IEnumerable singleton = this.GetSingletonDecoratedCollection(CreateCollection);

                // Passing the enumerable type is needed when running in a (Silverlight) sandbox.
                return(Expression.Constant(singleton, enumerableServiceType));
            }

            return(callExpression);
        }
예제 #2
0
        private Expression BuildDecoratorEnumerableExpressionForConstantEnumerable(
            Delegate wrapInstanceWithDecoratorDelegate, IEnumerable collection)
        {
            // Build the query: from item in collection select wrapInstanceWithDecorator(item);
            IEnumerable decoratedCollection =
                collection.Select(this.registeredServiceType, wrapInstanceWithDecoratorDelegate);

            // Passing the enumerable type is needed when running in the Silverlight sandbox.
            Type enumerableServiceType = typeof(IEnumerable <>).MakeGenericType(this.registeredServiceType);

            if (this.Lifestyle == Lifestyle.Singleton)
            {
                IEnumerable CreateCollection()
                {
                    Array array = ToArray(this.registeredServiceType, decoratedCollection);

                    return(DecoratorHelpers.MakeReadOnly(this.registeredServiceType, array));
                }

                IEnumerable singleton = this.GetSingletonDecoratedCollection(CreateCollection);

                return(Expression.Constant(singleton, enumerableServiceType));
            }

            return(Expression.Constant(decoratedCollection, enumerableServiceType));
        }