public IStorageProvider CreateStorageProvider() { var ctorArguments = GetConstructorArguments(); var ctor = (from constructor in ProviderType.GetConstructors() let parameters = constructor.GetParameters() where (parameters.Length == ctorArguments.Length) && (parameters.All(parameter => parameter.ParameterType.IsInstanceOfType(ctorArguments[parameter.Position]))) select constructor).First(); return((IStorageProvider)ctor.Invoke(ctorArguments)); }
private ConstructorInfo GetBestMatchingConstructor() { var constructors = (from ctor in ProviderType.GetConstructors() let parameters = ctor.GetParameters() where parameters.Length == ConstructorParameters.Count where parameters.All(p => ConstructorParameters.ContainsKey(p.Name)) select ctor).ToArray(); if (!constructors.Any()) { throw new ConfigurationErrorsException( String.Format( "Type {0} doesn't contain a public constructor with {1} parameters", ProviderType, ConstructorParameters.Count)); } if (constructors.Length == 1) { return(constructors.Single()); } throw new ConfigurationErrorsException(String.Format("Multiple constructors matched on type {0}", ProviderType)); }