bool TryCreate(RegistrationConstructor ctor, object[] parameters) { for (int i = 0; i < parameters.Length; i++) { Registration reg; if (!_Registrations.TryGetValue(ctor.Parameters[i].ParameterType, out reg) || reg.Instance == null) { return(false); } parameters[i] = reg.Instance; } ctor.Registration.Instance = Invoke(ctor.Constructor, parameters, ctor.Registration.Type); return(true); }
Exception GetSpecificError(LinkedList <RegistrationConstructor> theRest, bool autoRegisterMissing) { var missing = new HashSet <Type>(); foreach (var ctor in theRest) { foreach (var param in ctor.Parameters) { if (!_Registrations.ContainsKey(param.ParameterType)) { missing.Add(param.ParameterType); } } } if (autoRegisterMissing) { bool progress = false; // see if we can create anything from missing foreach (var t in missing) { if (t.IsInterface || t.IsAbstract) { continue; } var maybe = new Registration(this, t, null); _Registrations[t] = maybe; progress = true; var kvp = maybe.Create(); if (kvp.Key == null) { // success _Unique.AddLast(maybe); continue; } // better luck next time var ctor = new RegistrationConstructor(kvp.Key, kvp.Value, maybe); theRest.AddLast(ctor); } if (progress) { return(null); } } var incomplete = theRest .Select(c => new KeyValuePair <Type, ParameterInfo[]>(c.Registration.Type, c.Parameters)) .ToList(); var created = _Unique .Where(r => !r.ExternalOwned && r.Instance != null) .Select(r => r.Instance) .ToList(); return(missing.Any() ? (DependencyException) new DependencyMissingException(created, missing.ToList(), incomplete) : new DependencyCycleException(created, incomplete)); }