diff --git a/VContainer.SourceGenerator/VContainerSourceGenerator.cs b/VContainer.SourceGenerator/VContainerSourceGenerator.cs index dfb53b44..5bb61524 100644 --- a/VContainer.SourceGenerator/VContainerSourceGenerator.cs +++ b/VContainer.SourceGenerator/VContainerSourceGenerator.cs @@ -218,16 +218,16 @@ static bool TryEmitInjectMethod( foreach (var fieldSymbol in typeMeta.InjectFields) { - var fieldTypeName = - fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); - codeWriter.AppendLine($"__x.{fieldSymbol.Name} = resolver.Resolve<{fieldTypeName}>();"); + var fieldTypeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + + codeWriter.AppendLine($"__x.{fieldSymbol.Name} = ({fieldTypeName})resolver.ResolveOrParameter(typeof({fieldTypeName}), \"{fieldSymbol.Name}\", parameters);"); } foreach (var propSymbol in typeMeta.InjectProperties) { - var propTypeName = - propSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); - codeWriter.AppendLine($"__x.{propSymbol.Name} = resolver.Resolve<{propTypeName}>();"); + var propTypeName = propSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + + codeWriter.AppendLine($"__x.{propSymbol.Name} = ({propTypeName})resolver.ResolveOrParameter(typeof({propTypeName}), \"{propSymbol.Name}\", parameters);"); } foreach (var methodSymbol in typeMeta.InjectMethods) diff --git a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs index bab7c8ec..61ea2221 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs @@ -23,8 +23,8 @@ public static ReflectionInjector Build(Type type) [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Inject(object instance, IObjectResolver resolver, IReadOnlyList parameters) { - InjectFields(instance, resolver); - InjectProperties(instance, resolver); + InjectFields(instance, resolver, parameters); + InjectProperties(instance, resolver, parameters); InjectMethods(instance, resolver, parameters); } @@ -56,26 +56,26 @@ public object CreateInstance(IObjectResolver resolver, IReadOnlyList parameters) { if (injectTypeInfo.InjectFields == null) return; foreach (var x in injectTypeInfo.InjectFields) { - var fieldValue = resolver.Resolve(x.FieldType); + var fieldValue = resolver.ResolveOrParameter(x.FieldType, x.Name, parameters); x.SetValue(obj, fieldValue); } } - void InjectProperties(object obj, IObjectResolver resolver) + void InjectProperties(object obj, IObjectResolver resolver, IReadOnlyList parameters) { if (injectTypeInfo.InjectProperties == null) return; foreach (var x in injectTypeInfo.InjectProperties) { - var propValue = resolver.Resolve(x.PropertyType); + var propValue = resolver.ResolveOrParameter(x.PropertyType, x.Name, parameters); x.SetValue(obj, propValue); } }