Beispiel #1
0
            public Expression Build(IBuildContext context, Expression expression)
            {
                // Builds expression using base lifetime
                expression = _baseSingletonLifetime.Build(context, expression);

                // Defines `this` variable to store the reference to the current lifetime instance to call internal method 'IncrementCounter'
                var thisVar = Expression.Constant(this);

                // Creates a code block
                return(Expression.Block(
                           // Adds the expression to call the method 'IncrementCounter' for the current lifetime instance
                           Expression.Call(thisVar, IncrementCounterMethodInfo),
                           // Returns the expression to create an instance
                           expression));
            }
Beispiel #2
0
        public static Resolver <T> Compile <T>([NotNull] this ILifetime lifetime, [NotNull] Expression <Func <T> > lambdaExpression)
        {
            if (lifetime == null)
            {
                throw new ArgumentNullException(nameof(lifetime));
            }
            if (lambdaExpression == null)
            {
                throw new ArgumentNullException(nameof(lambdaExpression));
            }
            var buildContext       = new BuildContext(TypeDescriptor <T> .Key, Mock.Of <IContainer>(), new List <IDisposable>(), new List <IBuilder>(), DefaultAutowiringStrategy.Shared);
            var lifetimeExpression = lifetime.Build(lambdaExpression.Body, buildContext);
            var resolverExpression = Expression.Lambda(buildContext.Key.Type.ToResolverType(), lifetimeExpression, false, WellknownExpressions.ResolverParameters);

            return((Resolver <T>)ExpressionCompiler.Shared.Compile(resolverExpression));
        }
        public static Resolver <T> Compile <T>([NotNull] this ILifetime lifetime, [NotNull] Expression <Func <T> > lambdaExpression, IBuildContext context = null)
        {
            if (lifetime == null)
            {
                throw new ArgumentNullException(nameof(lifetime));
            }
            if (lambdaExpression == null)
            {
                throw new ArgumentNullException(nameof(lambdaExpression));
            }
            var buildContext = new BuildContext(null, new Key(typeof(T)), Mock.Of <IContainer>(), new List <ICompiler>()
            {
                DefaultCompiler.Shared
            }, new List <IBuilder>(), DefaultAutowiringStrategy.Shared, Registration.ArgsParameter, Registration.ContainerParameter, Mock.Of <IObserver <ContainerEvent> >());
            var lifetimeExpression = lifetime.Build(buildContext, lambdaExpression.Body);
            var resolverExpression = Expression.Lambda(buildContext.Key.Type.ToResolverType(), lifetimeExpression, false, Registration.ResolverParameters);

            DefaultCompiler.Shared.TryCompile(context ?? Mock.Of <IBuildContext>(), resolverExpression, out var resolverDelegate, out _);
            return((Resolver <T>)resolverDelegate);
        }