IReadOnlyList <ServiceCacheKey> GetCandidateCacheKeys(ServiceRegistrationKey registrationKey) { var cacheKeys = ServiceCacheKey.CreateFromRegistrationKey(registrationKey); var output = new List <ServiceCacheKey>(); lock (syncRoot) { foreach (var cacheKey in cacheKeys) { if (instances.ContainsKey(cacheKey)) { output.Add(cacheKey); } if (registrationKey.Name == null) { var otherMatchingKeys = instances.Keys .Where(x => cacheKey.ImplementationType.GetTypeInfo().IsAssignableFrom(x.ImplementationType.GetTypeInfo())); output.AddRange(otherMatchingKeys); } } return(output.OrderByDescending(x => x, specificityComparer).ToArray()); } }
/// <summary> /// Adds a component to the cache. /// </summary> /// <param name="registration">Registration.</param> /// <param name="instance">Instance.</param> public void Add(IServiceRegistration registration, object instance) { if (registration == null) { throw new ArgumentNullException(nameof(registration)); } var key = ServiceRegistrationKey.ForRegistration(registration); var cacheKeys = ServiceCacheKey.CreateFromRegistrationKeyAndInstance(key, instance); foreach (var cacheKey in cacheKeys) { instances.TryAdd(cacheKey, instance); } }