public static void SetCurrentOperationToSettingProperty(string propertyName, IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); context.CurrentOperation = new SettingPropertyOperation( BuildKey.GetType(context.BuildKey), propertyName); }
public static void SetCurrentOperationToResolvingParameter(string parameterName, string methodSignature, IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); context.CurrentOperation = new MethodArgumentResolveOperation( BuildKey.GetType(context.BuildKey), methodSignature, parameterName); }
public static void SetCurrentOperationToInvokingConstructor(string constructorSignature, IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); context.CurrentOperation = new InvokingConstructorOperation( BuildKey.GetType(context.BuildKey), constructorSignature); }
/// <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)); } }
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))); }
/// <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); }
/// <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)); }
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)); } } }
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())); }
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); }
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); }
/// <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)); } } }
/// <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); }