コード例 #1
0
        private object BuildUpNewObject(IBuilderContext context, Type typeToBuild, object existing, string idToBuild)
        {
            ICreationPolicy policy = context.Policies.Get <ICreationPolicy>(typeToBuild, idToBuild);

            if (policy == null)
            {
                if (idToBuild == null)
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                              Properties.Resources.MissingPolicyUnnamed, typeToBuild));
                }
                else
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                                                              Properties.Resources.MissingPolicyNamed, typeToBuild, idToBuild));
                }
            }

            try
            {
                existing = FormatterServices.GetSafeUninitializedObject(typeToBuild);
            }
            catch (MemberAccessException exception)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.CannotCreateInstanceOfType, typeToBuild), exception);
            }

            RegisterObject(context, typeToBuild, existing, idToBuild);
            InitializeObject(context, existing, idToBuild, policy);
            return(existing);
        }
コード例 #2
0
        public override object BuildUp(IBuilderContext context,
                                       object buildKey,
                                       object existing)
        {
            ICreationPolicy creationPolicy = context.Policies.Get <ICreationPolicy>(buildKey);
            IInterfaceInterceptionPolicy interceptionPolicy = context.Policies.Get <IInterfaceInterceptionPolicy>(buildKey);
            Type typeToBuild;

            if (creationPolicy != null &&
                creationPolicy.SupportsReflection &&
                interceptionPolicy != null &&
                TryGetTypeFromBuildKey(buildKey, out typeToBuild))
            {
                ConstructorInfo ctor       = creationPolicy.GetConstructor(context, buildKey);
                object[]        ctorParams = creationPolicy.GetParameters(context, ctor);
                Type            originalType;

                if (!TryGetTypeFromBuildKey(context.OriginalBuildKey, out originalType))
                {
                    originalType = typeToBuild;
                }

                buildKey = InterceptInterface(context, typeToBuild, originalType, interceptionPolicy, ctor, ctorParams);
            }

            return(base.BuildUp(context, buildKey, existing));
        }
コード例 #3
0
        public void ZeroConstructorsOnReferenceType()
        {
            MockBuilderContext            context  = new MockBuilderContext();
            ConstructorReflectionStrategy strategy = new ConstructorReflectionStrategy();

            strategy.BuildUp(context, typeof(ZeroClass), null);

            ICreationPolicy policy = context.Policies.Get <ICreationPolicy>(typeof(ZeroClass));

            Assert.NotNull(policy);
            Assert.IsType <ZeroClass>(policy.Create(context, typeof(ZeroClass)));
        }
コード例 #4
0
        static object BuildUpNewObject(IBuilderContext context,
                                       object buildKey)
        {
            ICreationPolicy policy = context.Policies.Get <ICreationPolicy>(buildKey);

            if (policy == null)
            {
                throw new InvalidOperationException("Could not find creation policy for build key " + buildKey);
            }

            return(policy.Create(context, buildKey));
        }
コード例 #5
0
        public void OneDecoratedConstructor()
        {
            MockBuilderContext            context  = new MockBuilderContext();
            ConstructorReflectionStrategy strategy = new ConstructorReflectionStrategy();

            strategy.BuildUp(context, typeof(Decorated), null);

            ICreationPolicy policy    = context.Policies.Get <ICreationPolicy>(typeof(Decorated));
            Decorated       decorated = Assert.IsType <Decorated>(policy.Create(context, typeof(Decorated)));

            Assert.True(decorated.Constructor__Called);
        }
コード例 #6
0
        static object BuildUpNewObject(IBuilderContext context)
        {
            ICreationPolicy policy = context.Policies.Get <ICreationPolicy>(context.BuildKey);

            if (policy == null)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Resources.NoCreationPolicy,
                              context.BuildKey));
            }

            return(policy.Create(context, context.BuildKey));
        }
コード例 #7
0
        public void OneDecoratedConstructor()
        {
            MockBuilderContext context = new MockBuilderContext();
            ConstructorReflectionStrategy <InjectionConstructorAttribute, DependencyAttribute> strategy =
                new ConstructorReflectionStrategy <InjectionConstructorAttribute, DependencyAttribute>();

            context.Strategies.Add(strategy);

            context.ExecuteBuildUp(typeof(Decorated), null);

            ICreationPolicy policy    = context.Policies.Get <ICreationPolicy>(typeof(Decorated));
            Decorated       decorated = AssertHelper.IsType <Decorated>(policy.Create(context, typeof(Decorated)));

            Assert.IsTrue(decorated.Constructor__Called);
        }
コード例 #8
0
        public void ZeroConstructorsOnReferenceType()
        {
            MockBuilderContext context = new MockBuilderContext();
            ConstructorReflectionStrategy <InjectionConstructorAttribute, DependencyAttribute> strategy =
                new ConstructorReflectionStrategy <InjectionConstructorAttribute, DependencyAttribute>();

            context.Strategies.Add(strategy);

            context.ExecuteBuildUp(typeof(ZeroClass), null);

            ICreationPolicy policy = context.Policies.Get <ICreationPolicy>(typeof(ZeroClass));

            Assert.IsNotNull(policy);
            AssertHelper.IsType <ZeroClass>(policy.Create(context, typeof(ZeroClass)));
        }
コード例 #9
0
        /// <summary>
        /// See <see cref="ReflectionStrategy{T}.GetMembers"/> for more information.
        /// </summary>
        protected override IEnumerable <IReflectionMemberInfo <ConstructorInfo> > GetMembers(IBuilderContext context,
                                                                                             Type typeToBuild, object existing,
                                                                                             string idToBuild)
        {
            List <IReflectionMemberInfo <ConstructorInfo> > result = new List <IReflectionMemberInfo <ConstructorInfo> >();
            ICreationPolicy existingPolicy = context.Policies.Get <ICreationPolicy>(typeToBuild, idToBuild);

            if (existing == null && (existingPolicy == null || existingPolicy is DefaultCreationPolicy))
            {
                ConstructorInfo   injectionCtor = null;
                ConstructorInfo[] ctors         = typeToBuild.GetConstructors();

                if (ctors.Length == 1)
                {
                    injectionCtor = ctors[0];
                }
                else
                {
                    foreach (ConstructorInfo ctor in ctors)
                    {
                        if (Attribute.IsDefined(ctor, typeof(InjectionConstructorAttribute)))
                        {
                            // Multiple decorated constructors aren't valid
                            if (injectionCtor != null)
                            {
                                throw new InvalidAttributeException();
                            }

                            injectionCtor = ctor;
                        }
                    }
                }

                if (injectionCtor != null)
                {
                    result.Add(new ReflectionMemberInfo <ConstructorInfo>(injectionCtor));
                }
            }

            return(result);
        }
コード例 #10
0
        public override object BuildUp(IBuilderContext context,
                                       object buildKey,
                                       object existing)
        {
            ICreationPolicy            creationPolicy     = context.Policies.Get <ICreationPolicy>(buildKey);
            IVirtualInterceptionPolicy interceptionPolicy = context.Policies.Get <IVirtualInterceptionPolicy>(buildKey);
            Type typeToBuild;

            if (creationPolicy != null &&
                creationPolicy.SupportsReflection &&
                interceptionPolicy != null &&
                TryGetTypeFromBuildKey(buildKey, out typeToBuild))
            {
                ConstructorInfo ctor       = creationPolicy.GetConstructor(context, buildKey);
                object[]        ctorParams = creationPolicy.GetParameters(context, ctor);

                buildKey = InterceptClass(context, typeToBuild, interceptionPolicy, ctorParams);
            }

            return(base.BuildUp(context, buildKey, existing));
        }
コード例 #11
0
        protected override IEnumerable <IMemberInfo <ConstructorInfo> > GetMembers(IBuilderContext context,
                                                                                   object buildKey,
                                                                                   object existing)
        {
            ICreationPolicy existingPolicy = context.Policies.GetNoDefault <ICreationPolicy>(buildKey, false);

            if (existing == null && existingPolicy == null)
            {
                Type              typeToBuild   = GetTypeFromBuildKey(buildKey);
                ConstructorInfo   injectionCtor = null;
                ConstructorInfo[] ctors         = typeToBuild.GetConstructors();

                if (ctors.Length == 1)
                {
                    injectionCtor = ctors[0];
                }
                else
                {
                    foreach (ConstructorInfo ctor in ctors)
                    {
                        if (Attribute.IsDefined(ctor, typeof(InjectionConstructorAttribute)))
                        {
                            if (injectionCtor != null)
                            {
                                throw new InvalidAttributeException(typeToBuild, ".ctor");
                            }

                            injectionCtor = ctor;
                        }
                    }
                }

                if (injectionCtor != null)
                {
                    yield return(new MethodMemberInfo <ConstructorInfo>(injectionCtor));
                }
            }
        }
コード例 #12
0
 private void InitializeObject(IBuilderContext context, object existing, string id, ICreationPolicy policy)
 {
     Type type = existing.GetType();
     ConstructorInfo constructor = policy.SelectConstructor(context, type, id);
     if (constructor == null)
     {
         if (!type.IsValueType)
         {
             throw new ArgumentException(Resources.NoAppropriateConstructor);
         }
     }
     else
     {
         object[] parameters = policy.GetParameters(context, type, id, constructor);
         MethodBase methodInfo = constructor;
         Guard.ValidateMethodParameters(methodInfo, parameters, existing.GetType());
         if (base.TraceEnabled(context))
         {
             base.TraceBuildUp(context, type, id, Resources.CallingConstructor, new object[] { base.ParametersToTypeList(parameters) });
         }
         methodInfo.Invoke(existing, parameters);
     }
 }
コード例 #13
0
        private static void InitializeObject(IBuilderContext context, object existing, string id, ICreationPolicy policy)
        {
            Type type = existing.GetType();
            ConstructorInfo constructor = policy.SelectConstructor(context, type, id);

            if (constructor == null)
            {
                if (type.IsValueType)
                    return;
                throw new ArgumentException(Resources.NoAppropriateConstructor);
            }

            object[] parms = policy.GetParameters(context, type, id, constructor);

            MethodBase method = (MethodBase) constructor;
            ValidateMethodParameters(method, parms, existing.GetType());
            method.Invoke(existing, parms);
        }
コード例 #14
0
 public BulletPool(ICreationPolicy <T> _creationPolicy) : this()
 {
     m_CreationPolicy = _creationPolicy;
 }
コード例 #15
0
        private void InitializeObject(IBuilderContext context, object existing, string id, ICreationPolicy policy)
        {
            Type            type        = existing.GetType();
            ConstructorInfo constructor = policy.SelectConstructor(context, type, id);

            if (constructor == null)
            {
                if (type.IsValueType)
                {
                    return;
                }
                throw new ArgumentException(Properties.Resources.NoAppropriateConstructor);
            }

            object[] parms = policy.GetParameters(context, type, id, constructor);

            MethodBase method = (MethodBase)constructor;

            Guard.ValidateMethodParameters(method, parms, existing.GetType());

            if (TraceEnabled(context))
            {
                TraceBuildUp(context, type, id, Properties.Resources.CallingConstructor, ParametersToTypeList(parms));
            }

            method.Invoke(existing, parms);
        }
コード例 #16
0
        private void InitializeObject(IBuilderContext context, object existing, string id, ICreationPolicy policy)
        {
            Type type = existing.GetType();
            ConstructorInfo constructor = policy.SelectConstructor(context, type, id);

            if (constructor == null)
            {
                if (type.IsValueType)
                    return;
                throw new ArgumentException(Properties.Resources.NoAppropriateConstructor);
            }

            object[] parms = policy.GetParameters(context, type, id, constructor);

            MethodBase method = (MethodBase)constructor;
            Guard.ValidateMethodParameters(method, parms, existing.GetType());

            if (TraceEnabled(context))
                TraceBuildUp(context, type, id, Properties.Resources.CallingConstructor, ParametersToTypeList(parms));

            method.Invoke(existing, parms);
        }
コード例 #17
0
 public ObjectPool(ICreationPolicy <T> _creationPolicy) : this()
 {
     m_CreationPolicy = _creationPolicy;
 }
コード例 #18
0
        private static void InitializeObject(IBuilderContext context, object existing, string id, ICreationPolicy policy)
        {
            Type            type        = existing.GetType();
            ConstructorInfo constructor = policy.SelectConstructor(context, type, id);

            if (constructor == null)
            {
                if (type.IsValueType)
                {
                    return;
                }
                throw new ArgumentException(Resources.NoAppropriateConstructor);
            }

            object[] parms = policy.GetParameters(context, type, id, constructor);

            MethodBase method = (MethodBase)constructor;

            ValidateMethodParameters(method, parms, existing.GetType());
            method.Invoke(existing, parms);
        }