Example #1
0
        /// <summary>
        /// Create a new <see cref="GenericResolvedArrayParameter"/> instance that specifies
        /// that the given named generic parameter should be resolved.
        /// </summary>
        /// <param name="genericParameterName">The generic parameter name to resolve.</param>
        /// <param name="elementValues">The values for the elements, that will
        /// be converted to <see cref="InjectionParameterValue"/> objects.</param>
        public GenericResolvedArrayParameter(string genericParameterName, params object[] elementValues)
        {
            Guard.ArgumentNotNull(genericParameterName, "genericParameterName");

            this.genericParameterName = genericParameterName;
            this.elementValues.AddRange(ToParameters(elementValues));
        }
Example #2
0
 /// <summary>
 /// RegisterType a type mapping with the container, where the created instances will use
 /// the given <see cref="LifetimeManager"/>.
 /// </summary>
 /// <param name="from"><see cref="Type"/> that will be requested.</param>
 /// <param name="to"><see cref="Type"/> that will actually be returned.</param>
 /// <param name="name">Name to use for registration, null if a default registration.</param>
 /// <param name="lifetimeManager">The <see cref="LifetimeManager"/> that controls the lifetime
 /// of the returned instance.</param>
 /// <returns>The <see cref="UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic).</returns>
 public override IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager)
 {
     if (to != null && !from.IsGenericType && !to.IsGenericType)
     {
         Guard.TypeIsAssignable(from, to, "from");
     }
     registering(this, new RegisterEventArgs(from, to, name, lifetimeManager));
     return(this);
 }
Example #3
0
 /// <summary>
 /// RegisterType an instance with the container.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Instance registration is much like setting a type as a singleton, except that instead
 /// of the container creating the instance the first time it is requested, the user
 /// creates the instance ahead of type and adds that instance to the container.
 /// </para>
 /// </remarks>
 /// <param name="t">Type of instance to register (may be an implemented interface instead of the full type).</param>
 /// <param name="instance">Object to returned.</param>
 /// <param name="name">Name for registration.</param>
 /// <param name="lifetime">
 /// <para>If true, the container will take over the lifetime of the instance,
 /// calling Dispose on it (if it's <see cref="IDisposable"/>) when the container is Disposed.</para>
 /// <para>
 ///  If false, container will not maintain a strong reference to <paramref name="instance"/>. User is reponsible
 /// for disposing instance, and for keeping the instance from being garbage collected.</para></param>
 /// <returns>The <see cref="UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic).</returns>
 public override IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime)
 {
     Guard.ArgumentNotNull(instance, "instance");
     Guard.ArgumentNotNull(lifetime, "lifetime");
     Guard.TypeIsAssignable(t, instance.GetType(), "instance");
     registeringInstance(this,
                         new RegisterInstanceEventArgs(t,
                                                       instance,
                                                       name,
                                                       lifetime));
     return(this);
 }
        public override IDependencyResolverPolicy GetResolverPolicy(Type typeToBuild)
        {
            Guard.ArgumentNotNull(typeToBuild, "typeToBuild");
            ReflectionHelper parameterReflector = new ReflectionHelper(ParameterType);
            Type             typeToResolve      = parameterReflector.Type;

            if (parameterReflector.IsOpenGeneric)
            {
                typeToResolve = parameterReflector.GetClosedParameterType(typeToBuild.GetGenericArguments());
            }
            return(new NamedTypeDependencyResolverPolicy(typeToResolve, name));
        }
Example #5
0
        public override bool MatchesType(Type t)
        {
            Guard.ArgumentNotNull(t, "t");

            if (!t.IsArray || t.GetArrayRank() != 1)
            {
                return(false);
            }

            Type elementType = t.GetElementType();

            return(elementType.IsGenericParameter && elementType.Name == genericParameterName);
        }
Example #6
0
        public override void AddPolicies(Type typeToCreate, string name, IPolicyList policies)
        {
            Guard.ArgumentNotNull(typeToCreate, "typeToCreate");
            PropertyInfo propInfo = typeToCreate.GetProperty(propertyName);

            GuardPropertyExists(propInfo, typeToCreate, propertyName);
            GuardPropertyIsSettable(propInfo);
            GuardPropertyIsNotIndexer(propInfo);
            InitializeParameterValue(propInfo);
            GuardPropertyValueIsCompatible(propInfo, parameterValue);

            SpecifiedPropertiesSelectorPolicy selector =
                GetSelectorPolicy(policies, typeToCreate, name);

            selector.AddPropertyAndValue(propInfo, parameterValue);
        }
Example #7
0
        public override IDependencyResolverPolicy GetResolverPolicy(Type typeToBuild)
        {
            Guard.ArgumentNotNull(typeToBuild, "typeToBuild");

            var parameterReflector = new ReflectionHelper(ParameterType);

            if (parameterReflector.IsGenericArray)
            {
                return(this.CreateGenericArrayResolverPolicy(typeToBuild, parameterReflector));
            }

            if (parameterReflector.IsOpenGeneric || parameterReflector.Type.IsGenericParameter)
            {
                return(this.CreateGenericResolverPolicy(typeToBuild, parameterReflector));
            }

            return(this.CreateResolverPolicy(parameterReflector.Type));
        }
Example #8
0
 // FxCop warning suppressed: false positive, Guard class is doing validation
 public override object BuildUp(Type t, object existing, string name)
 {
     Guard.ArgumentNotNull(existing, "existing");
     Guard.TypeIsAssignable(t, existing.GetType(), "existing");
     return(DoBuildUp(t, existing, name));
 }
Example #9
0
 public override bool MatchesType(Type t)
 {
     Guard.ArgumentNotNull(t, "t");
     return(t.IsGenericParameter && t.Name == genericParameterName);
 }