Skip to content

Commit

Permalink
fix: ServiceContainer find type
Browse files Browse the repository at this point in the history
  • Loading branch information
labbbirder committed Oct 7, 2023
1 parent 53bdd07 commit adefc05
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions Runtime/ClassicalUsages/ServiceContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,53 @@ public static void ClearInstances()
{
singletons.Clear();
}
public static object Get(Type type)
static Type FindType(Type type)
{
if (!lutInfos.TryGetValue(type, out var info))
{
var subtypes = Retriever.GetAllSubtypes(type)
.Append(type)
.Where(t => !t.IsInterface)
.Where(t => !t.IsAbstract)
.ToArray()
;
if (subtypes.Length == 0)
var resultType = type;
if (type.IsAbstract || type.IsInterface)
{
throw new ArgumentException($"type {type} doesn't has an implement");
var subtypes = Retriever.GetAllSubtypes(type)
.Append(type)
.Where(t => !t.IsInterface)
.Where(t => !t.IsAbstract)
.ToArray()
;
if (subtypes.Length == 0)
{
throw new ArgumentException($"type {type} doesn't has an implement");
}
if (subtypes.Length > 1)
{
Debug.LogWarning($"type {type} exists more than one implements");
}
resultType = subtypes[0];
}
if (subtypes.Length > 1)

//TODO:Construct generic type recursively
//TODO:Take multiple generic type constraints into account
if (resultType.IsGenericType && resultType.IsGenericTypeDefinition)
{
Debug.LogWarning($"type {type} exists more than one implements");
var typeParams = resultType.GenericTypeArguments
.Select(a => a.GetGenericParameterConstraints()[0])
.Select(FindType)
.ToArray()
;
resultType = resultType.MakeGenericType(typeParams);
}
lutInfos[type] = info = new Info()
{
resultType = subtypes[0],
resultType = resultType,
scopeMode = DefaultScopeMode,
};
}
return info.resultType;
}
public static object Get(Type type)
{
FindType(type);
var info = lutInfos[type];
if (info.scopeMode == Single && singletons.TryGetValue(type, out var existing))
{
return existing;
Expand Down

0 comments on commit adefc05

Please sign in to comment.