예제 #1
0
        /// <summary>
        /// Add resolver to composition.
        /// </summary>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public ResolverComposition Add(IResolver resolver)
        {
            if (resolver == null)
            {
                throw new ArgumentNullException(nameof(resolver));
            }
            if (isReadonly)
            {
                throw new InvalidOperationException("read-only");
            }

            resolvers.Add(resolver);

            // Add to resolvers by type
            Type[] intfs = resolver.GetType().GetInterfaces();
            foreach (Type itype in intfs.Where(i => i.IsGenericType && typeof(IResolver <>).Equals(i.GetGenericTypeDefinition())))
            {
                Type[] paramTypes  = itype.GetGenericArguments();
                var    resolveType = paramTypes[0];
                resolversByType.Add(resolveType, resolver);
            }

            // Add to resolvers by parameter name
            if (resolver is IParameterResolver parameterResolver_)
            {
                foreach (string parameterName in parameterResolver_.ParameterNames)
                {
                    resolversByParameterName.Add(parameterName, parameterResolver_);
                }
            }

            // Add to generic resolvers
            if (resolver is IGenericResolver genericResolver)
            {
                genericResolvers.Add(genericResolver);
            }

            // Update parameter names
            List <string> names = new List <string>();

            foreach (var r in resolvers)
            {
                if (r is IParameterResolver parameterResolver)
                {
                    names.AddRange(parameterResolver.ParameterNames);
                }
            }
            ParameterNames = names.Distinct().ToArray();
            return(this);
        }