コード例 #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 static CreateInstance CreateConstructorExpression(IContainerContext containerContext, ResolutionConstructor resolutionConstructor,
            ResolutionMember[] members = null)
        {
            var strategyParameter = Expression.Constant(containerContext.ResolutionStrategy, typeof(IResolutionStrategy));
            var resolutionInfoParameter = Expression.Parameter(typeof(ResolutionInfo), "resolutionInfo");

            var arguments = CreateExpressionFromResolutionTargets(resolutionConstructor.Parameters, strategyParameter, resolutionInfoParameter);

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

            if (members == null || members.Length == 0)
                return Expression.Lambda<CreateInstance>(newExpression, resolutionInfoParameter).Compile();

            var length = members.Length;
            var propertyExpressions = new MemberBinding[length];
            for (var i = 0; i < length; i++)
            {
                var member = members[i];
                var propertyExpression = Expression.Bind(member.MemberInfo,
                    CreateResolutionTargetExpression(member.ResolutionTarget, strategyParameter,
                        resolutionInfoParameter));
                propertyExpressions[i] = propertyExpression;

            }

            var initExpression = Expression.MemberInit(newExpression, propertyExpressions);
            return Expression.Lambda<CreateInstance>(initExpression, resolutionInfoParameter).Compile();
        }
コード例 #3
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);
        }
コード例 #4
0
 public bool TryChooseConstructor(out ResolutionConstructor resolutionConstructor, ResolutionInfo resolutionInfo, InjectionParameter[] injectionParameters = null)
 {
     return this.TryGetBestConstructor(out resolutionConstructor, resolutionInfo, injectionParameters);
 }
コード例 #5
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;
        }
コード例 #6
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);
 }
コード例 #7
0
 private Expression GetExpressionInternal(ResolutionConstructor constructor, ResolutionInfo resolutionInfo, Expression resolutionInfoExpression)
 {
     return ExpressionDelegateFactory.CreateExpression(this.containerContext, constructor, resolutionInfo, resolutionInfoExpression, this.GetResolutionMembers());
 }
コード例 #8
0
 public Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType)
 {
     if (this.resolutionConstructor != null && !this.isConstructorDirty) return this.GetExpressionInternal(this.resolutionConstructor, resolutionInfo, resolutionInfoExpression);
     {
         lock (this.syncObject)
         {
             if (this.resolutionConstructor != null && !this.isConstructorDirty) return this.GetExpressionInternal(this.resolutionConstructor, resolutionInfo, resolutionInfoExpression);
             {
                 ResolutionConstructor constructor;
                 if (!this.metaInfoProvider.TryChooseConstructor(out constructor, injectionParameters: this.injectionParameters))
                     throw new ResolutionFailedException(this.metaInfoProvider.TypeTo.FullName);
                 this.resolutionConstructor = constructor;
                 this.isConstructorDirty = false;
                 return this.GetExpressionInternal(constructor, resolutionInfo, resolutionInfoExpression);
             }
         }
     }
 }