Skip to content

Commit

Permalink
Merge pull request #203 from hadashiA/ku/misc
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA authored May 1, 2021
2 parents 17b735a + d780eb6 commit 1c63815
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void AddToBuildBuffer(IDictionary<Type, IRegistration> buf, Type service,
}
else
{
collection = new CollectionRegistration(service) { exists };
collection = new CollectionRegistration(collectionService, service) { exists };
AddCollectionToBuildBuffer(buf, collection);
}
collection.Add(registration);
Expand Down Expand Up @@ -97,14 +97,14 @@ public bool TryGet(Type interfaceType, out IRegistration registration)
if (interfaceType.IsConstructedGenericType)
{
var genericType = interfaceType.GetGenericTypeDefinition();
return TryGetCollection(interfaceType, genericType, out registration);
return TryFallbackSingleCollection(interfaceType, genericType, out registration);
}
return false;
}

public bool Exists(Type type) => hashTable.TryGet(type, out _);

bool TryGetCollection(Type interfaceType, Type genericType, out IRegistration registration)
bool TryFallbackSingleCollection(Type interfaceType, Type genericType, out IRegistration registration)
{
if (genericType == typeof(IEnumerable<>) ||
genericType == typeof(IReadOnlyList<>))
Expand Down
10 changes: 8 additions & 2 deletions VContainer/Assets/VContainer/Runtime/Internal/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ sealed class CollectionRegistration : IRegistration, IEnumerable<IRegistration>
readonly List<Type> interfaceTypes;
readonly IList<IRegistration> registrations = new List<IRegistration>();

public CollectionRegistration(Type elementType)
public CollectionRegistration(Type elementType) : this(
typeof(List<>).MakeGenericType(elementType),
elementType)
{
}

public CollectionRegistration(Type listType, Type elementType)
{
this.elementType = elementType;
ImplementationType = typeof(List<>).MakeGenericType(elementType);
ImplementationType = listType;
interfaceTypes = new List<Type>
{
typeof(IEnumerable<>).MakeGenericType(elementType),
Expand Down
1 change: 1 addition & 0 deletions VContainer/Assets/VContainer/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public void ResolveGenerics()

var resolved = container.Resolve<GenericsArgumentService>();
Assert.That(resolved.GenericsService, Is.InstanceOf<GenericsService<I2>>());
Assert.That(resolved.GenericsService.ParameterService, Is.InstanceOf<NoDependencyServiceA>());
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions VContainer/Assets/VContainer/Tests/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ public void MethodInjectable1(I2 service2)

class GenericsService<T>
{
public readonly I2 Service2;
public readonly I2 ParameterService;

public GenericsService(I2 service2)
public GenericsService(I2 parameterService)
{
Service2 = service2;
ParameterService = parameterService;
}
}

Expand Down

0 comments on commit 1c63815

Please sign in to comment.