Example #1
0
            private object GetInjectedInterceptedAndInitializedInstanceInternal()
            {
                Expression expression =
                    SingletonLifestyle.BuildConstantExpression(this.instance, this.ImplementationType);

                // NOTE: We pass on producer.ServiceType as the implementation type for the following three
                // methods. This will the initialization to be only done based on information of the service
                // type; not on that of the implementation. Although now the initialization could be
                // incomplete, this behavior is consistent with the initialization of
                // Register<TService>(Func<TService>, Lifestyle), which doesn't have the proper static type
                // information available to use the implementation type.
                // TODO: This behavior should be reconsidered, because now it is incompatible with
                // Register<TService, TImplementation>(Lifestyle). So the question is, do we consider
                // RegisterInstance<TService>(TService) to be similar to Register<TService>(Func<TService>)
                // or to Register<TService, TImplementation>()? See: #353.
                expression = this.WrapWithPropertyInjector(this.ServiceType, expression);
                expression = this.InterceptInstanceCreation(this.ServiceType, expression);
                expression = this.WrapWithInitializer(this.ServiceType, expression);

                // PERF: We don't need to compile a delegate in case all we have is a constant.
                if (expression is ConstantExpression constantExpression)
                {
                    return(constantExpression.Value);
                }

                Delegate initializer = Expression.Lambda(expression).Compile();

                // This delegate might return a different instance than the originalInstance (caused by a
                // possible interceptor).
                return(initializer.DynamicInvoke());
            }
        internal static Registration CreateUncontrolledCollectionRegistration(Type itemType,
                                                                              IEnumerable collection, Container container)
        {
            Type enumerableType = typeof(IEnumerable <>).MakeGenericType(itemType);

            var registration =
                SingletonLifestyle.CreateSingleInstanceRegistration(enumerableType, collection, container);

            registration.IsCollection = true;

            return(registration);
        }
Example #3
0
 public override Expression BuildExpression() =>
 SingletonLifestyle.BuildConstantExpression(
     this.GetInterceptedInstance(), this.ImplementationType);