示例#1
0
        /// <summary>
        /// Removes the first service in <see cref="IRegistrationCollection"/> with the same service type
        /// as <paramref name="descriptor"/> and adds <paramef name="descriptor"/> to the collection.
        /// </summary>
        /// <param name="collection">The <see cref="IRegistrationCollection"/>.</param>
        /// <param name="descriptor">The <see cref="RegistrationDescriptor"/> to replace with.</param>
        /// <returns></returns>
        public static IRegistrationCollection Replace(
            this IRegistrationCollection collection,
            RegistrationDescriptor descriptor)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            var registeredRegistrationDescriptor = collection.FirstOrDefault(s => s.ServiceType == descriptor.ServiceType);

            if (registeredRegistrationDescriptor is object)
            {
                collection.Remove(registeredRegistrationDescriptor);
            }

            collection.Add(descriptor);
            return(collection);
        }