From 8cddb212029f20a810a8248d0a199c2967497086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=84=E3=82=82?= Date: Tue, 6 Dec 2022 16:57:27 +0900 Subject: [PATCH 1/2] Allow parameters to be passed in field injection and property injection as well --- .../Runtime/Internal/ReflectionInjector.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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); } } From c794affe6ab200274ec80d841dd3055f69c940cc Mon Sep 17 00:00:00 2001 From: Alon Talmi <130899578+AlonTalmi@users.noreply.github.com> Date: Thu, 16 Nov 2023 12:59:55 +0100 Subject: [PATCH 2/2] Added parameters support to fields and properties in code generation --- .../VContainerSourceGenerator.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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)