示例#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);
        }
        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));
        }
        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);
        }
        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);
        }
        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));
        }
        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);
     }
 }
        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);
        }
        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);
        }
 public ObjectPool(ICreationPolicy <T> _creationPolicy) : this()
 {
     m_CreationPolicy = _creationPolicy;
 }
        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);
        }