Пример #1
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>
        /// <param name="injectionMembers">Injection configuration objects.</param>
        /// <returns>The <see cref="UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic).</returns>
        public IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
        {
            Guard.ArgumentNotNull(to, "to");
            Guard.ArgumentNotNull(injectionMembers, "injectionMembers");

            if (string.IsNullOrEmpty(name))
            {
                name = null;
            }

            if (from != null && !from.GetTypeInfo().IsGenericType&& !to.GetTypeInfo().IsGenericType)
            {
                Guard.TypeIsAssignable(from, to, "from");
            }

            Registering(this, new RegisterEventArgs(from, to, name, lifetimeManager));

            if (injectionMembers.Length > 0)
            {
                ClearExistingBuildPlan(to, name);
                foreach (var member in injectionMembers)
                {
                    member.AddPolicies(from, to, name, policies);
                }
            }
            return(this);
        }
Пример #2
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 responsible
 /// 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 IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime)
 {
     Guard.ArgumentNotNull(instance, "instance");
     Guard.ArgumentNotNull(lifetime, "lifetime");
     Guard.InstanceIsAssignable(t, instance, "instance");
     RegisteringInstance(this,
                         new RegisterInstanceEventArgs(t,
                                                       instance,
                                                       name,
                                                       lifetime));
     return(this);
 }
Пример #3
0
        public override void PreBuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");
            Type typeToBuild = context.BuildKey.Type;

            if (typeToBuild.IsArray && typeToBuild.GetArrayRank() == 1)
            {
                Type elementType = typeToBuild.GetElementType();

                MethodInfo resolverMethod = GenericResolveArrayMethod.MakeGenericMethod(elementType);

                ArrayResolver resolver = (ArrayResolver)resolverMethod.CreateDelegate(typeof(ArrayResolver));

                context.Existing      = resolver(context);
                context.BuildComplete = true;
            }
        }
Пример #4
0
        public void Teardown(object o)
        {
            IBuilderContext context = null;

            try
            {
                Guard.ArgumentNotNull(o, "o");

                context =
                    new BuilderContext(GetStrategies().Reverse(), lifetimeContainer, policies, null, o);
                context.Strategies.ExecuteTearDown(context);
            }
            catch (Exception ex)
            {
                throw new ResolutionFailedException(o.GetType(), null, ex, context);
            }
        }
Пример #5
0
 public object BuildUp(Type t, object existing, string name, params ResolverOverride[] resolverOverrides)
 {
     Guard.ArgumentNotNull(existing, "existing");
     Guard.InstanceIsAssignable(t, existing, "existing");
     return(DoBuildUp(t, existing, name, resolverOverrides));
 }
Пример #6
0
        /// <summary>
        /// Return instances of all registered types requested.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method is useful if you've registered multiple types with the same
        /// <see cref="Type"/> but different names.
        /// </para>
        /// <para>
        /// Be aware that this method does NOT return an instance for the default (unnamed) registration.
        /// </para>
        /// </remarks>
        /// <param name="t">The type requested.</param>
        /// <param name="resolverOverrides">Any overrides for the resolve calls.</param>
        /// <returns>Set of objects of type <paramref name="t"/>.</returns>
        public IEnumerable <object> ResolveAll(Type t, params ResolverOverride[] resolverOverrides)
        {
            Guard.ArgumentNotNull(t, "t");

            return((IEnumerable <object>) this.Resolve(t.MakeArrayType(), resolverOverrides));
        }