From e3a996576c78671c8e488583d3b7d6844699cdee Mon Sep 17 00:00:00 2001 From: Yuri Sokolov <7556847+migus88@users.noreply.github.com> Date: Mon, 4 Aug 2025 09:18:26 +0300 Subject: [PATCH 1/3] fixed: component no longer can be created with `new` keyword with reflection --- .../Assets/VContainer/Runtime/Internal/ReflectionInjector.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs index 62f5db26..c0aad6fa 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs @@ -31,6 +31,11 @@ public void Inject(object instance, IObjectResolver resolver, IReadOnlyList parameters) { + if (typeof(UnityEngine.Component).IsAssignableFrom(injectTypeInfo.Type)) + { + throw new NotSupportedException($"UnityEngine.Component:{injectTypeInfo.Type.Name} cannot be `new`"); + } + var parameterInfos = injectTypeInfo.InjectConstructor.ParameterInfos; var parameterKeys = injectTypeInfo.InjectConstructor.ParameterKeys; var parameterValues = CappedArrayPool.Shared8Limit.Rent(parameterInfos.Length); From 26272bdd13c90913116593200d97582e3e44ad98 Mon Sep 17 00:00:00 2001 From: Yuri Sokolov <7556847+migus88@users.noreply.github.com> Date: Mon, 4 Aug 2025 09:21:51 +0300 Subject: [PATCH 2/3] removed unused using --- .../Assets/VContainer/Runtime/Internal/ReflectionInjector.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs index c0aad6fa..230a748d 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Reflection; namespace VContainer.Internal { From df1f60bca4d26b65da85636cf4822e9df2066263 Mon Sep 17 00:00:00 2001 From: Yuri Sokolov <7556847+migus88@users.noreply.github.com> Date: Mon, 4 Aug 2025 09:30:07 +0300 Subject: [PATCH 3/3] added unity define to wrap unity specific code --- .../Assets/VContainer/Runtime/Internal/ReflectionInjector.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs index 230a748d..8b1847da 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/ReflectionInjector.cs @@ -30,11 +30,12 @@ public void Inject(object instance, IObjectResolver resolver, IReadOnlyList parameters) { +#if UNITY_2020_1_OR_NEWER if (typeof(UnityEngine.Component).IsAssignableFrom(injectTypeInfo.Type)) { throw new NotSupportedException($"UnityEngine.Component:{injectTypeInfo.Type.Name} cannot be `new`"); } - +#endif var parameterInfos = injectTypeInfo.InjectConstructor.ParameterInfos; var parameterKeys = injectTypeInfo.InjectConstructor.ParameterKeys; var parameterValues = CappedArrayPool.Shared8Limit.Rent(parameterInfos.Length);