-
-
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.
Merge pull request #602 from hadashiA/ku/fix-error
FIx for unity older version
- Loading branch information
Showing
3 changed files
with
48 additions
and
57 deletions.
There are no files selected for viewing
68 changes: 35 additions & 33 deletions
68
...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,65 +1,67 @@ | ||
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()); | ||
readonly Func<Type[], Registration> createRegistrationFunc; | ||
|
||
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>(); | ||
createRegistrationFunc = CreateRegistration; | ||
} | ||
|
||
public Registration GetClosedRegistration(Type closedInterfaceType, Type[] typeParameters) | ||
{ | ||
var typeParametersHash = typeParametersHashes.GetOrAdd(closedInterfaceType, static (_, arg) => | ||
((IStructuralEquatable)arg).GetHashCode(EqualityComparer<Type>.Default), typeParameters); | ||
|
||
var registrationArgs = new RegistrationArguments | ||
{ | ||
ImplementationType = implementationType, | ||
Lifetime = lifetime, | ||
CustomParameters = customParameters, | ||
TypeParameters = typeParameters | ||
}; | ||
|
||
return registrations.GetOrAdd(typeParametersHash, static (_, args) => | ||
CreateRegistration(args), registrationArgs); | ||
return constructedRegistrations.GetOrAdd(typeParameters, createRegistrationFunc); | ||
} | ||
|
||
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) |
c5eb818
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
vcontainer – ./
vcontainer-hadashia.vercel.app
vcontainer.vercel.app
vcontainer-git-master-hadashia.vercel.app
vcontainer.hadashikick.jp