public override void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     base.GetObjectData(info, context);
     info.AddValue("ExecutingStrategy", ExecutingStrategy.GetType(), typeof(Type));
     info.AddValue("ExecutingStrategyIndex", ExecutingStrategyIndex, typeof(int));
     info.AddValue("BuildKey", BuildKey.ToString(), typeof(string));
 }
        public static void SetCurrentOperationToInvokingConstructor(string constructorSignature, IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");

            context.CurrentOperation = new InvokingConstructorOperation(
                BuildKey.GetType(context.BuildKey), constructorSignature);
        }
 public static void SetCurrentOperationToResolvingParameter(string parameterName, string methodSignature, IBuilderContext context)
 {
     Guard.ArgumentNotNull(context, "context");
     context.CurrentOperation = new MethodArgumentResolveOperation(
         BuildKey.GetType(context.BuildKey),
         methodSignature, parameterName);
 }
Пример #4
0
        public static void SetCurrentOperationToSettingProperty(string propertyName, IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");

            context.CurrentOperation = new SettingPropertyOperation(
                BuildKey.GetType(context.BuildKey), propertyName);
        }
 /// <summary>
 /// Retrieves the list of methods to iterate looking for injection attributes.
 /// </summary>
 /// <param name="context">The build context.</param>
 /// <returns>
 /// An enumerable wrapper around the <see cref="IMemberInfo{MethodInfo}"/> interfaces that
 /// represent the members to be inspected for reflection.
 /// </returns>
 protected override IEnumerable <IMemberInfo <MethodInfo> > GetMembers(IBuilderContext context)
 {
     foreach (MethodInfo method in BuildKey.GetType(context.BuildKey).GetMethods())
     {
         yield return(new MethodMemberInfo <MethodInfo>(method));
     }
 }
Пример #6
0
 public static void ThrowForNullExistingObject(IBuilderContext context)
 {
     Guard.ArgumentNotNull(context, "context");
     throw new InvalidOperationException(
               string.Format(CultureInfo.CurrentCulture,
                             Resources.NoConstructorFound,
                             BuildKey.GetType(context.BuildKey).Name));
 }
        /// <summary>
        /// Create the object for the given <paramref name="context"/> and <paramref name="buildKey"/>.
        /// </summary>
        /// <param name="context">The builder context.</param>
        /// <param name="buildKey">The key for the object being built.</param>
        /// <returns>The created object.</returns>
        public object Create(IBuilderContext context,
                             object buildKey)
        {
            Guard.ArgumentNotNull(context, "context");

            return(Activator.CreateInstance(BuildKey.GetType(buildKey),
                                            GetParameters(context, null)));
        }
Пример #8
0
 /// <summary>
 /// Retrieves the list of properties to iterate looking for injection attributes.
 /// </summary>
 /// <param name="context">The build context.</param>
 /// <returns>
 /// An enumerable wrapper around the <see cref="IMemberInfo{PropertyInfo}"/> interfaces that
 /// represent the members to be inspected for reflection.
 /// </returns>
 protected override IEnumerable <IMemberInfo <PropertyInfo> > GetMembers(IBuilderContext context)
 {
     foreach (PropertyInfo propInfo in BuildKey.GetType(context.BuildKey).GetProperties())
     {
         if (propInfo.GetIndexParameters().Length == 0)
         {
             yield return(new PropertyMemberInfo(propInfo));
         }
     }
 }
        /// <summary>
        /// Choose the constructor to call for the given type.
        /// </summary>
        /// <param name="context">Current build context.</param>
        /// <returns>The chosen constructor.</returns>
        ///<exception cref="InvalidOperationException">Thrown when the constructor to choose is ambiguous.</exception>
        public virtual SelectedConstructor SelectConstructor(IBuilderContext context)
        {
            Type            typeToConstruct = BuildKey.GetType(context.BuildKey);
            ConstructorInfo ctor            = FindInjectionConstructor(typeToConstruct) ?? FindLongestConstructor(typeToConstruct);

            if (ctor != null)
            {
                return(CreateSelectedConstructor(context, ctor));
            }
            return(null);
        }
Пример #10
0
        /// <summary>
        /// Maps the build key.
        /// </summary>
        /// <param name="buildKey">The build key to map.</param>
        /// <returns>The new build key.</returns>
        public object Map(object buildKey)
        {
            Type originalType = BuildKey.GetType(buildKey);

            GuardSameNumberOfGenericArguments(originalType);

            Type[] genericArguments = originalType.GetGenericArguments();
            Type   resultType       = BuildKey.GetType(destinationKey).MakeGenericType(genericArguments);

            return(BuildKey.ReplaceType(destinationKey, resultType));
        }
Пример #11
0
 public GenericTypeBuildKeyMappingPolicy(object destinationKey)
 {
     if (!BuildKey.GetType(destinationKey).IsGenericTypeDefinition)
     {
         throw new ArgumentException(
                   string.Format(CultureInfo.CurrentCulture,
                                 Resources.MustHaveOpenGenericType,
                                 BuildKey.GetType(destinationKey).Name));
     }
     this.destinationKey = destinationKey;
 }
        /// <summary>
        /// Return the sequence of methods to call while building the target object.
        /// </summary>
        /// <param name="context">Current build context.</param>
        /// <returns>Sequence of methods to call.</returns>
        public virtual IEnumerable <SelectedMethod> SelectMethods(IBuilderContext context)
        {
            Type t = BuildKey.GetType(context.BuildKey);

            foreach (MethodInfo method in t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy))
            {
                if (method.IsDefined(typeof(TMarkerAttribute), false))
                {
                    yield return(CreateSelectedMethod(context, method));
                }
            }
        }
Пример #13
0
 public static void ThrowForAttemptingToConstructInterface(IBuilderContext context)
 {
     Guard.ArgumentNotNull(context, "context");
     if (BuildKey.GetType(context.BuildKey).IsInterface)
     {
         throw new InvalidOperationException(
                   string.Format(CultureInfo.CurrentCulture,
                                 Resources.CannotConstructInterface,
                                 BuildKey.GetType(context.BuildKey),
                                 context.BuildKey));
     }
 }
        /// <summary>
        /// Returns sequence of properties on the given type that
        /// should be set as part of building that object.
        /// </summary>
        /// <param name="context">current build context.</param>
        /// <returns>Sequence of <see cref="PropertyInfo"/> objects
        /// that contain the properties to set.</returns>
        public virtual IEnumerable <SelectedProperty> SelectProperties(IBuilderContext context)
        {
            Type t = BuildKey.GetType(context.BuildKey);

            foreach (PropertyInfo prop in t.GetProperties(BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance))
            {
                if (prop.GetIndexParameters().Length == 0 &&             // Ignore indexers
                    prop.CanWrite &&                                     // Is writable
                    prop.IsDefined(typeof(TResolutionAttribute), false)) // Marked with the attribute
                {
                    yield return(CreateSelectedProperty(context, prop));
                }
            }
        }
        /// <summary>
        /// Construct a build plan.
        /// </summary>
        /// <param name="context">The current build context.</param>
        /// <param name="buildKey">The current build key.</param>
        /// <returns>The created build plan.</returns>
        public IBuildPlanPolicy CreatePlan(IBuilderContext context, object buildKey)
        {
            IDynamicBuilderMethodCreatorPolicy methodCreatorPolicy =
                context.Policies.Get <IDynamicBuilderMethodCreatorPolicy>(context.BuildKey);
            DynamicBuildPlanGenerationContext generatorContext =
                new DynamicBuildPlanGenerationContext(
                    BuildKey.GetType(buildKey), methodCreatorPolicy);

            IBuilderContext planContext = GetContext(context, buildKey, generatorContext);

            planContext.Strategies.ExecuteBuildUp(planContext);

            return(new DynamicMethodBuildPlan(generatorContext.GetBuildMethod()));
        }
Пример #16
0
        private static ILifetimePolicy GetLifetimePolicy(IBuilderContext context)
        {
            ILifetimePolicy policy = context.Policies.GetNoDefault <ILifetimePolicy>(context.BuildKey, false);

            if (policy == null && BuildKey.GetType(context.BuildKey).IsGenericType)
            {
                policy = GetLifetimePolicyForGenericType(context);
            }

            if (policy == null)
            {
                policy = new TransientLifetimePolicy();
                context.PersistentPolicies.Set <ILifetimePolicy>(policy, context.BuildKey);
            }
            return(policy);
        }
Пример #17
0
        /// <summary>
        /// Called during the chain of responsibility for a build operation. The
        /// PreBuildUp method is called when the chain is being executed in the
        /// forward direction.
        /// </summary>
        /// <param name="context">Context of the build operation.</param>
        // FxCop suppression: Validation is done by Guard class
        public override void PreBuildUp(IBuilderContext context)
        {
            Type type;

            BuildKey.TryGetType(context.BuildKey, out type);

            foreach (IMemberInfo <TMemberInfo> member in GetMembers(context))
            {
                if (MemberRequiresProcessing(member))
                {
                    IEnumerable <IParameter> parameters =
                        GenerateIParametersFromParameterInfos(member.GetParameters(), type, member.Name);

                    AddParametersToPolicy(context, member, parameters);
                }
            }
        }
Пример #18
0
        private static ILifetimePolicy GetLifetimePolicyForGenericType(IBuilderContext context)
        {
            Type   typeToBuild         = BuildKey.GetType(context.BuildKey);
            object openGenericBuildKey =
                BuildKey.ReplaceType(context.BuildKey, typeToBuild.GetGenericTypeDefinition());

            ILifetimeFactoryPolicy factoryPolicy =
                context.Policies.Get <ILifetimeFactoryPolicy>(openGenericBuildKey);

            if (factoryPolicy != null)
            {
                ILifetimePolicy lifetime = factoryPolicy.CreateLifetimePolicy();
                context.PersistentPolicies.Set <ILifetimePolicy>(lifetime, context.BuildKey);
                return(lifetime);
            }

            return(null);
        }
Пример #19
0
        /// <summary>
        /// Gets an individual policy.
        /// </summary>
        /// <param name="policyInterface">The interface the policy is registered under.</param>
        /// <param name="buildKey">The key the policy applies.</param>
        /// <param name="localOnly">true if the policy searches local only; otherwise false to seach up the parent chain.</param>
        /// <returns>The policy in the list, if present; returns null otherwise.</returns>
        public IBuilderPolicy Get(Type policyInterface,
                                  object buildKey,
                                  bool localOnly)
        {
            Type buildType;

            if (!BuildKey.TryGetType(buildKey, out buildType) || !buildType.IsGenericType)
            {
                return
                    (GetNoDefault(policyInterface, buildKey, localOnly) ??
                     GetNoDefault(policyInterface, null, localOnly));
            }

            return
                (GetNoDefault(policyInterface, buildKey, localOnly) ??
                 GetNoDefault(policyInterface, BuildKey.ReplaceType(buildKey, buildType.GetGenericTypeDefinition()), localOnly) ??
                 GetNoDefault(policyInterface, null, localOnly));
        }
Пример #20
0
        /// <summary>
        /// Retrieves the list of constructors to iterate looking for injection attributes.
        /// </summary>
        /// <param name="context">The build context.</param>
        /// <returns>
        /// An enumerable wrapper around the <see cref="IMemberInfo{ConstructorInfo}"/> interfaces that
        /// represent the members to be inspected for reflection.
        /// </returns>
        protected override IEnumerable <IMemberInfo <ConstructorInfo> > GetMembers(IBuilderContext context)
        {
            ICreationPolicy existingPolicy = context.Policies.GetNoDefault <ICreationPolicy>(context.BuildKey, false);

            if (context.Existing == null && existingPolicy == null)
            {
                Type              typeToBuild   = BuildKey.GetType(context.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(TInjectionConstructorAttribute)))
                        {
                            if (injectionCtor != null)
                            {
                                throw new InvalidAttributeException(typeToBuild, ".ctor");
                            }

                            injectionCtor = ctor;
                        }
                    }
                }

                if (injectionCtor != null)
                {
                    yield return(new MethodMemberInfo <ConstructorInfo>(injectionCtor));
                }
            }
        }
Пример #21
0
 /// <summary>
 /// Gets the <see cref="Type"/> of object to build from the <paramref name="buildKey"/>.
 /// </summary>
 /// <param name="buildKey">The key of object to be built.</param>
 /// <returns>The <see cref="Type"/> of object to be built.</returns>
 protected virtual Type GetTypeFromBuildKey(object buildKey)
 {
     return(BuildKey.GetType(buildKey));
 }
 public static void SetCurrentOperationToInvokingMethod(string methodSignature, IBuilderContext context)
 {
     Guard.ArgumentNotNull(context, "context");
     context.CurrentOperation = new InvokingMethodOperation(BuildKey.GetType(context.BuildKey), methodSignature);
 }