-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
56 deletions.
There are no files selected for viewing
65 changes: 33 additions & 32 deletions
65
...ainer/Assets/VContainer/Runtime/Internal/InstanceProviders/OpenGenericInstanceProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,65 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
|
||
namespace VContainer.Internal | ||
{ | ||
public class OpenGenericInstanceProvider : IInstanceProvider | ||
{ | ||
class TypeParametersEqualityComparer : IEqualityComparer<Type[]> | ||
{ | ||
public bool Equals(Type[] x, Type[] y) | ||
{ | ||
if (x == null || y == null) return x == y; | ||
if (x.Length != y.Length) return false; | ||
|
||
for (var i = 0; i < x.Length; i++) | ||
{ | ||
if (x[i] != y[i]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
public int GetHashCode(Type[] typeParameters) | ||
{ | ||
var hash = 5381; | ||
foreach (var typeParameter in typeParameters) | ||
{ | ||
hash = ((hash << 5) + hash) ^ typeParameter.GetHashCode(); | ||
} | ||
return hash; | ||
} | ||
} | ||
|
||
readonly Lifetime lifetime; | ||
readonly Type implementationType; | ||
readonly IReadOnlyList<IInjectParameter> customParameters; | ||
|
||
readonly ConcurrentDictionary<int, Registration> registrations; | ||
readonly ConcurrentDictionary<Type, int> typeParametersHashes; | ||
readonly ConcurrentDictionary<Type[], Registration> constructedRegistrations = new ConcurrentDictionary<Type[], Registration>(new TypeParametersEqualityComparer()); | ||
|
||
public OpenGenericInstanceProvider(Type implementationType, Lifetime lifetime, | ||
List<IInjectParameter> injectParameters) | ||
public OpenGenericInstanceProvider(Type implementationType, Lifetime lifetime, List<IInjectParameter> injectParameters) | ||
{ | ||
this.implementationType = implementationType; | ||
this.lifetime = lifetime; | ||
customParameters = injectParameters; | ||
typeParametersHashes = new ConcurrentDictionary<Type, int>(); | ||
registrations = new ConcurrentDictionary<int, Registration>(); | ||
} | ||
|
||
public Registration GetClosedRegistration(Type closedInterfaceType, Type[] typeParameters) | ||
{ | ||
var typeParametersHash = typeParametersHashes.GetOrAdd(closedInterfaceType, (_, arg) => | ||
((IStructuralEquatable)arg).GetHashCode(EqualityComparer<Type>.Default), typeParameters); | ||
|
||
var registrationArgs = new RegistrationArguments | ||
{ | ||
ImplementationType = implementationType, | ||
Lifetime = lifetime, | ||
CustomParameters = customParameters, | ||
TypeParameters = typeParameters | ||
}; | ||
|
||
return registrations.GetOrAdd(typeParametersHash, (_, args) => CreateRegistration(args), registrationArgs); | ||
return constructedRegistrations.GetOrAdd(typeParameters, typeParameters => CreateRegistration(typeParameters)); | ||
} | ||
|
||
private static Registration CreateRegistration(RegistrationArguments args) | ||
Registration CreateRegistration(Type[] typeParameters) | ||
{ | ||
var newType = args.ImplementationType.MakeGenericType(args.TypeParameters); | ||
var newType = implementationType.MakeGenericType(typeParameters); | ||
var injector = InjectorCache.GetOrBuild(newType); | ||
var spawner = new InstanceProvider(injector, args.CustomParameters); | ||
return new Registration(newType, args.Lifetime, new List<Type>(1) { newType }, spawner); | ||
var spawner = new InstanceProvider(injector, customParameters); | ||
return new Registration(newType, lifetime, new List<Type>(1) { newType }, spawner); | ||
} | ||
|
||
public object SpawnInstance(IObjectResolver resolver) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
private struct RegistrationArguments | ||
{ | ||
public Type ImplementationType; | ||
public Lifetime Lifetime; | ||
public IReadOnlyList<IInjectParameter> CustomParameters; | ||
public Type[] TypeParameters; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
m_EditorVersion: 2022.3.5f1 | ||
m_EditorVersionWithRevision: 2022.3.5f1 (9674261d40ee) | ||
m_EditorVersion: 2021.3.30f1 | ||
m_EditorVersionWithRevision: 2021.3.30f1 (b4360d7cdac4) |