예제 #1
0
        /// <inheritdoc cref="Populator.Populate" />
        /// <exception cref="InvalidOperationException">Class type must not be static or abstract.</exception>
        public override IUnityContainer Populate(IUnityContainer container, IList <Type> typesWithAttribute)
        {
            foreach (Type to in typesWithAttribute)
            {
                RegisterTypeAttribute attribute = to.GetCustomAttribute <RegisterTypeAttribute>();
                Type   from = attribute.From;
                string name = attribute.Name;
                ITypeLifetimeManager lifetimeManager = attribute.LifetimeManager == null
                                                           ? null
                                                           : GetInstanceByType <ITypeLifetimeManager>(attribute.LifetimeManager);

                container.RegisterType(from, to, name, lifetimeManager);
            }

            return(container);
        }
        /// <summary>
        ///     Populate the passed <paramref name="container" />.
        /// </summary>
        /// <param name="container"><see cref="IUnityContainer" /> to populate.</param>
        /// <returns>Passed <paramref name="container" />.</returns>
        /// <exception cref="InvalidOperationException">Class type must not be static or abstract.</exception>
        public override IUnityContainer Populate(IUnityContainer container)
        {
            IList <Type> typesWithAttribute = GetTypesWith <RegisterTypeAttribute>(TypeDefined.Inherit)
                                              .ToList();

            foreach (Type to in typesWithAttribute)
            {
                if (to.IsStatic() || to.IsAbstract)
                {
                    throw new InvalidOperationException(
                              $"Class type must not be static or abstract to be used with RegisterTypeAttribute: {to.FullName}");
                }

                RegisterTypeAttribute attribute       = to.GetCustomAttribute <RegisterTypeAttribute>();
                ITypeLifetimeManager  lifetimeManager = attribute.LifetimeManager == null
                                                           ? null
                                                           : GetInstanceByType <ITypeLifetimeManager>(attribute.LifetimeManager);

                container.RegisterType(attribute.From, to, lifetimeManager);
            }

            return(container);
        }