コード例 #1
0
        public static Expression CreateExpression(IContainerContext containerContext, ResolutionConstructor resolutionConstructor, ResolutionInfo resolutionInfo,
            Expression resolutionInfoExpression, ResolutionMember[] members = null)
        {
            var length = resolutionConstructor.Parameters.Length;
            var arguments = new Expression[length];

            for (var i = 0; i < length; i++)
            {
                var parameter = resolutionConstructor.Parameters[i];
                arguments[i] = containerContext.ResolutionStrategy.GetExpressionForResolutionTarget(parameter, resolutionInfo, resolutionInfoExpression);
            }

            var newExpression = Expression.New(resolutionConstructor.Constructor, arguments);

            if (members == null || members.Length == 0) return newExpression;
            {
                var propLength = members.Length;
                var propertyExpressions = new MemberBinding[propLength];
                for (var i = 0; i < propLength; i++)
                {
                    var member = members[i];
                    var propertyExpression = Expression.Bind(member.MemberInfo,
                        containerContext.ResolutionStrategy.GetExpressionForResolutionTarget(member.ResolutionTarget, resolutionInfo, resolutionInfoExpression));
                    propertyExpressions[i] = propertyExpression;
                }

                return Expression.MemberInit(newExpression, propertyExpressions);
            }
        }
コード例 #2
0
        public override Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression)
        {
            var length = registrationCache.Length;
            var enumerableItems = new Expression[length];
            for (var i = 0; i < length; i++)
            {
                enumerableItems[i] = registrationCache[i].GetExpression(resolutionInfo, resolutionInfoExpression, this.enumerableType);
            }

            return Expression.NewArrayInit(this.enumerableType.Type, enumerableItems);
        }
コード例 #3
0
        /// <summary>
        /// Gets the instance managed by the <see cref="SingletonLifetime"/>
        /// </summary>
        /// <param name="objectBuilder">An <see cref="IObjectBuilder"/> implementation.</param>
        /// <param name="resolutionInfo">The info about the actual resolution.</param>
        /// <param name="resolveType">The type info about the resolved type.</param>
        /// <returns>The lifetime managed object.</returns>
        public object GetInstance(IObjectBuilder objectBuilder, ResolutionInfo resolutionInfo, TypeInformation resolveType)
        {
            if (this.instance != null) return this.instance;
            lock (this.syncObject)
            {
                if (this.instance != null) return this.instance;
                this.instance = objectBuilder.BuildInstance(resolutionInfo, resolveType);
            }

            return this.instance;
        }
コード例 #4
0
        public override object Resolve(ResolutionInfo resolutionInfo)
        {
            if (this.resolverDelegate != null) return this.resolverDelegate(resolutionInfo);
            lock (this.syncObject)
            {
                if (this.resolverDelegate != null) return this.resolverDelegate(resolutionInfo);
                var parameter = Expression.Parameter(typeof(ResolutionInfo));
                this.resolverDelegate = Expression.Lambda<ResolverDelegate>(this.GetExpression(resolutionInfo, parameter), parameter).Compile();
            }

            return this.resolverDelegate(resolutionInfo);
        }
コード例 #5
0
        public object BuildInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType)
        {
            if (this.builtInstance != null) return this.builtInstance;
            lock (this.syncObject)
            {
                if (this.builtInstance != null) return this.builtInstance;
                this.builtInstance = this.objectExtender.FillResolutionMembers(this.instance, this.containerContext, resolutionInfo);
                this.builtInstance = this.objectExtender.FillResolutionMethods(this.builtInstance, this.containerContext, resolutionInfo);
                this.builtInstance = this.containerExtensionManager.ExecutePostBuildExtensions(this.builtInstance, this.instanceType, this.containerContext, resolutionInfo, resolveType);
            }

            return this.builtInstance;
        }
コード例 #6
0
ファイル: ObjectExtender.cs プロジェクト: z4kn4fein/stashbox
        public object FillResolutionMethods(object instance, IContainerContext containerContext, ResolutionInfo resolutionInfo)
        {
            if (!this.metaInfoProvider.HasInjectionMethod) return instance;
            {
                var methods = this.GetResolutionMethods();

                var count = methods.Length;
                for (var i = 0; i < count; i++)
                {
                    methods[i].MethodDelegate(resolutionInfo, instance);
                }
            }

            return instance;
        }
コード例 #7
0
ファイル: ObjectExtender.cs プロジェクト: z4kn4fein/stashbox
        public object FillResolutionMembers(object instance, IContainerContext containerContext, ResolutionInfo resolutionInfo)
        {
            if (!this.metaInfoProvider.HasInjectionMembers) return instance;

            var members = this.GetResolutionMembers();

            var count = members.Length;
            for (var i = 0; i < count; i++)
            {
                var value = containerContext.ResolutionStrategy.EvaluateResolutionTarget(members[i].ResolutionTarget, resolutionInfo);
                members[i].MemberSetter(instance, value);
            }

            return instance;
        }
コード例 #8
0
        public Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType)
        {
            IServiceRegistration registration;
            if (this.containerContext.RegistrationRepository
                .TryGetRegistrationWithConditionsWithoutGenericDefinitionExtraction(resolveType, out registration))
                return registration.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType);

            lock (this.syncObject)
            {
                if (this.containerContext.RegistrationRepository
                    .TryGetRegistrationWithConditionsWithoutGenericDefinitionExtraction(resolveType, out registration))
                    return registration.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType);

                var genericType = this.metaInfoProvider.TypeTo.MakeGenericType(resolveType.Type.GenericTypeArguments);
                this.containerContext.Container.RegisterType(resolveType.Type, genericType);

                this.containerContext.RegistrationRepository
                    .TryGetRegistrationWithConditionsWithoutGenericDefinitionExtraction(resolveType, out registration);

                return registration.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType);
            }
        }
コード例 #9
0
        public object BuildInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType)
        {
            if (this.metaInfoProvider.HasInjectionMethod || this.containerExtensionManager.HasPostBuildExtensions)
            {
                if (this.constructorDelegate != null && !this.isConstructorDirty) return this.ResolveType(containerContext, resolutionInfo, resolveType);

                lock (this.syncObject)
                {
                    if (this.constructorDelegate != null && !this.isConstructorDirty) return this.ResolveType(containerContext, resolutionInfo, resolveType);

                    ResolutionConstructor constructor;
                    if (!this.metaInfoProvider.TryChooseConstructor(out constructor, resolutionInfo,
                            this.injectionParameters))
                        throw new ResolutionFailedException(this.metaInfoProvider.TypeTo.FullName);
                    this.constructorDelegate = ExpressionDelegateFactory.CreateConstructorExpression(this.containerContext, constructor, this.GetResolutionMembers());
                    this.resolutionConstructor = constructor;
                    this.isConstructorDirty = false;
                    return this.ResolveType(containerContext, resolutionInfo, resolveType);
                }

            }

            if (this.createDelegate != null && !this.isConstructorDirty) return this.createDelegate(resolutionInfo);
            this.createDelegate = this.containerContext.DelegateRepository.GetOrAdd(this.registrationName, () =>
            {
                ResolutionConstructor constructor;
                if (!this.metaInfoProvider.TryChooseConstructor(out constructor, resolutionInfo,
                    this.injectionParameters))
                    throw new ResolutionFailedException(this.metaInfoProvider.TypeTo.FullName);

                var parameter = Expression.Parameter(typeof(ResolutionInfo));
                this.createDelegate = Expression.Lambda<Func<ResolutionInfo, object>>(this.GetExpressionInternal(constructor, resolutionInfo, parameter), parameter).Compile();
                this.resolutionConstructor = constructor;
                this.isConstructorDirty = false;
                return this.createDelegate;
            }, this.isConstructorDirty);

            return this.createDelegate(resolutionInfo);
        }
コード例 #10
0
        public object BuildInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType)
        {
            object instance = null;

            if (this.singleFactory != null)
                instance = this.singleFactory.Invoke();

            if (this.oneParamsFactory != null)
                instance = this.oneParamsFactory.Invoke(resolutionInfo.FactoryParams.ElementAt(0));

            if (this.twoParamsFactory != null)
                instance = this.twoParamsFactory.Invoke(resolutionInfo.FactoryParams.ElementAt(0), resolutionInfo.FactoryParams.ElementAt(1));

            if (this.threeParamsFactory != null)
                instance = this.threeParamsFactory.Invoke(
                    resolutionInfo.FactoryParams.ElementAt(0),
                    resolutionInfo.FactoryParams.ElementAt(1),
                    resolutionInfo.FactoryParams.ElementAt(2));

            var builtInstance = this.objectExtender.FillResolutionMembers(instance, containerContext, resolutionInfo);
            builtInstance = this.objectExtender.FillResolutionMembers(builtInstance, containerContext, resolutionInfo);
            return this.containerExtensionManager.ExecutePostBuildExtensions(builtInstance, builtInstance?.GetType(), containerContext, resolutionInfo, resolveType);
        }
コード例 #11
0
ファイル: ContainerTests.cs プロジェクト: z4kn4fein/stashbox
 public override Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression)
 {
     throw new NotImplementedException();
 }
コード例 #12
0
 /// <summary>
 /// Gets the expression for getting the instance managed by the <see cref="SingletonLifetime"/>
 /// </summary>
 /// <param name="objectBuilder">An <see cref="IObjectBuilder"/> implementation.</param>
 /// <param name="resolutionInfo">The info about the actual resolution.</param>
 /// <param name="resolutionInfoExpression">The expression of the info about the actual resolution.</param>
 /// <param name="resolveType">The type info about the resolved type.</param>
 /// <returns>The lifetime managed object.</returns>
 public Expression GetExpression(IObjectBuilder objectBuilder, ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType)
 {
     return Expression.Constant(this.GetInstance(objectBuilder, resolutionInfo, resolveType));
 }
コード例 #13
0
 public override object Resolve(ResolutionInfo resolutionInfo)
 {
     return registrationCache.GetInstance(resolutionInfo, base.TypeInfo);
 }
コード例 #14
0
 public object EvaluateResolutionTarget(ResolutionTarget resolutionTarget,
     ResolutionInfo resolutionInfo)
 {
     return this.resolutionStrategy.EvaluateResolutionTarget(resolutionTarget, resolutionInfo);
 }
コード例 #15
0
 public Expression GetExpressionForResolutionTarget(ResolutionTarget resolutionTarget, ResolutionInfo resolutionInfo, Expression resolutionInfoExpression)
 {
     return this.resolutionStrategy.GetExpressionForResolutionTarget(resolutionTarget, resolutionInfo, resolutionInfoExpression);
 }
コード例 #16
0
        public object ExecutePostBuildExtensions(object instance, Type targetType, IContainerContext containerContext, ResolutionInfo resolutionInfo, 
            TypeInformation resolveType, InjectionParameter[] injectionParameters = null)
        {
            if (!this.hasPostBuildExtensions) return instance;
            using (this.readerWriterLock.AcquireReadLock())
            {
                var result = instance;
                foreach (var extension in this.postbuildExtensions)
                    result = extension.PostBuild(instance, targetType, containerContext, resolutionInfo, resolveType, injectionParameters);

                return result;
            }
        }
コード例 #17
0
 public Expression GetExpressionForResolutionTarget(ResolutionTarget resolutionTarget, ResolutionInfo resolutionInfo, Expression resolutionInfoExpression)
 {
     if (resolutionInfo.OverrideManager != null &&
         resolutionInfo.OverrideManager.ContainsValue(resolutionTarget.TypeInformation))
         return this.CreateOverrideExpression(resolutionTarget, resolutionInfoExpression);
     return resolutionTarget.ResolutionTargetValue != null ? Expression.Constant(resolutionTarget.ResolutionTargetValue) :
         resolutionTarget.Resolver.GetExpression(resolutionInfo, resolutionInfoExpression);
 }
コード例 #18
0
 public bool TryChooseConstructor(out ResolutionConstructor resolutionConstructor, ResolutionInfo resolutionInfo, InjectionParameter[] injectionParameters = null)
 {
     return this.TryGetBestConstructor(out resolutionConstructor, resolutionInfo, injectionParameters);
 }
コード例 #19
0
 private bool TryGetBestConstructor(out ResolutionConstructor resolutionConstructor, ResolutionInfo resolutionInfo,
     InjectionParameter[] injectionParameters = null)
 {
     return this.TryGetConstructor(this.metaInfoCache.Constructors.Where(constructor => constructor.HasInjectionAttribute), out resolutionConstructor, resolutionInfo, injectionParameters) ||
         this.TryGetConstructor(this.metaInfoCache.Constructors.Where(constructor => !constructor.HasInjectionAttribute), out resolutionConstructor, resolutionInfo, injectionParameters);
 }
コード例 #20
0
 /// <summary>
 /// Gets the expression for getting the instance managed by the <see cref="TransientLifetime"/>
 /// </summary>
 /// <param name="objectBuilder">An <see cref="IObjectBuilder"/> implementation.</param>
 /// <param name="resolutionInfo">The info about the actual resolution.</param>
 /// <param name="resolutionInfoExpression">The expression of the info about the actual resolution.</param>
 /// <param name="resolveType">The type info about the resolved type.</param>
 /// <returns>The lifetime managed object.</returns>
 public Expression GetExpression(IObjectBuilder objectBuilder, ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType)
 {
     return objectBuilder.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType);
 }
コード例 #21
0
ファイル: Resolver.cs プロジェクト: z4kn4fein/stashbox
 /// <summary>
 /// Produces an expression for creating an instance.
 /// </summary>
 /// <param name="resolutionInfo">The info about the actual resolution.</param>
 /// <param name="resolutionInfoExpression">The expression of the info about the actual resolution.</param>
 /// <returns>The expression.</returns>
 public abstract Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression);
コード例 #22
0
 /// <summary>
 /// Gets the instance managed by the <see cref="TransientLifetime"/>
 /// </summary>
 /// <param name="objectBuilder">An <see cref="IObjectBuilder"/> implementation.</param>
 /// <param name="resolutionInfo">The info about the actual resolution.</param>
 /// <param name="resolveType">The type info about the resolved type.</param>
 /// <returns>The lifetime managed object.</returns>
 public object GetInstance(IObjectBuilder objectBuilder, ResolutionInfo resolutionInfo, TypeInformation resolveType)
 {
     return objectBuilder.BuildInstance(resolutionInfo, resolveType);
 }
コード例 #23
0
ファイル: Resolver.cs プロジェクト: z4kn4fein/stashbox
 /// <summary>
 /// Produces an instance.
 /// </summary>
 /// <param name="resolutionInfo">The info about the actual resolution.</param>
 /// <returns>The resolved object.</returns>
 public abstract object Resolve(ResolutionInfo resolutionInfo);
コード例 #24
0
ファイル: ContainerTests.cs プロジェクト: z4kn4fein/stashbox
 public override object Resolve(ResolutionInfo resolutionInfo)
 {
     return new Test1();
 }
コード例 #25
0
 public Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType)
 {
     var callExpression = Expression.Call(Expression.Constant(this), "BuildInstance", null, resolutionInfoExpression, Expression.Constant(resolveType));
     return Expression.Convert(callExpression, resolveType.Type);
 }
コード例 #26
0
 /// <summary>
 /// Creates an expression for creating the resolved instance.
 /// </summary>
 /// <param name="resolutionInfo">The info about the current resolution.</param>
 /// <param name="resolutionInfoExpression">The expression of the info about the current resolution.</param>
 /// <param name="resolveType">The resolve type.</param>
 /// <returns>The expression.</returns>
 public Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType)
 {
     return this.lifetimeManager.GetExpression(this.objectBuilder, resolutionInfo, resolutionInfoExpression, resolveType);
 }
コード例 #27
0
        private bool TryGetConstructor(IEnumerable<ConstructorInformation> constructors, out ResolutionConstructor resolutionConstructor,
            ResolutionInfo resolutionInfo, InjectionParameter[] injectionParameters = null)
        {
            var usableConstructors = this.GetUsableConstructors(constructors, resolutionInfo, injectionParameters).ToArray();

            if (usableConstructors.Any())
            {
                resolutionConstructor = this.CreateResolutionConstructor(this.SelectBestConstructor(usableConstructors), injectionParameters);
                return true;
            }

            resolutionConstructor = null;
            return false;
        }
コード例 #28
0
 /// <summary>
 /// Gets the resolved instance.
 /// </summary>
 /// <param name="resolutionInfo">The info about the current resolution.</param>
 /// <param name="resolveType">The resolve type.</param>
 /// <returns>The created object.</returns>
 public object GetInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType)
 {
     return this.lifetimeManager.GetInstance(this.objectBuilder, resolutionInfo, resolveType);
 }
コード例 #29
0
        private IEnumerable<ConstructorInformation> GetUsableConstructors(IEnumerable<ConstructorInformation> constructors, ResolutionInfo resolutionInfo,
            InjectionParameter[] injectionParameters = null)
        {
            if (resolutionInfo?.OverrideManager == null)
                return constructors
                    .Where(constructor => constructor.Parameters
                        .All(parameter => this.containerContext.ResolutionStrategy.CanResolve(this.containerContext, parameter,
                        injectionParameters)));

            return constructors
                .Where(constructor => constructor.Parameters
                    .All(parameter => this.containerContext.ResolutionStrategy.CanResolve(this.containerContext, parameter,
                        injectionParameters) ||
                         resolutionInfo.OverrideManager.ContainsValue(parameter)));
        }
コード例 #30
0
 public object EvaluateResolutionTarget(ResolutionTarget resolutionTarget, ResolutionInfo resolutionInfo)
 {
     if (resolutionInfo.OverrideManager != null && resolutionInfo.OverrideManager.ContainsValue(resolutionTarget.TypeInformation))
         return resolutionInfo.OverrideManager.GetOverriddenValue(resolutionTarget.TypeInformation);
     return resolutionTarget.ResolutionTargetValue ?? resolutionTarget.Resolver.Resolve(resolutionInfo);
 }