public bool TryGet(Type interfaceType, out IRegistration registration) { if (hashTable.TryGet(interfaceType, out registration)) { return(true); } // Auto falling back to collection from one if (interfaceType.IsGenericType) { // TODO: var genericType = interfaceType.GetGenericTypeDefinition(); if (genericType == typeof(IEnumerable <>) || genericType == typeof(IReadOnlyList <>)) { var elementType = interfaceType.GetGenericArguments()[0]; var collectionRegistration = new CollectionRegistration(elementType); // ReSharper disable once InconsistentlySynchronizedField if (hashTable.TryGet(elementType, out var elementRegistration)) { collectionRegistration.Add(elementRegistration); } registration = collectionRegistration; return(true); } } return(false); }
static void AddToBuildBuffer(IDictionary <Type, IRegistration> buf, Type service, IRegistration registration) { if (buf.TryGetValue(service, out var exists)) { var collectionService = typeof(IEnumerable <>).MakeGenericType(service); CollectionRegistration collection; if (buf.TryGetValue(collectionService, out var found)) { collection = (CollectionRegistration)found; } else { collection = new CollectionRegistration(service) { exists }; AddCollectionToBuildBuffer(buf, collection); } collection.Add(registration); } else { buf.Add(service, registration); } }
bool TryFallbackSingleCollection(Type interfaceType, Type genericType, out IRegistration registration) { if (genericType == typeof(IEnumerable <>) || genericType == typeof(IReadOnlyList <>)) { var elementType = interfaceType.GetGenericArguments()[0]; var collectionRegistration = new CollectionRegistration(elementType); // ReSharper disable once InconsistentlySynchronizedField if (hashTable.TryGet(elementType, out var elementRegistration) && elementRegistration != null) { collectionRegistration.Add(elementRegistration); } registration = collectionRegistration; return(true); } registration = null; return(false); }
static void AddCollectionToBuildBuffer(IDictionary <Type, IRegistration> buf, CollectionRegistration collectionRegistration) { foreach (var collectionType in collectionRegistration.InterfaceTypes) { try { buf.Add(collectionType, collectionRegistration); } catch (ArgumentException) { throw new VContainerException(collectionType, $"Registration with the same key already exists: {collectionType} {collectionRegistration}"); } } }
static void AddCollectionToBuildBuffer(IDictionary <Type, IRegistration> buf, CollectionRegistration collectionRegistration) { // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < collectionRegistration.InterfaceTypes.Count; i++) { var collectionType = collectionRegistration.InterfaceTypes[i]; try { buf.Add(collectionType, collectionRegistration); } catch (ArgumentException) { throw new VContainerException(collectionType, $"Registration with the same key already exists: {collectionRegistration}"); } } }