diff --git a/Runtime/TargetMethodsInfo.cs b/Runtime/TargetMethodsInfo.cs index f2eaedd..da8218e 100644 --- a/Runtime/TargetMethodsInfo.cs +++ b/Runtime/TargetMethodsInfo.cs @@ -12,13 +12,19 @@ internal class TargetMethodsInfo public TargetMethodsInfo(Type targetType) { - foreach (var methodInfo in targetType.GetMethods()) + foreach (var methodInfo in targetType.GetMethods(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic)) { if (methodInfo.GetCustomAttributes(typeof(InjectAttribute), true).Length > 0) + { + if (!methodInfo.IsPublic) + throw new Exception($"Inject method must be public. {targetType.Name}.{methodInfo.Name}()"); InjectMethods.Add(methodInfo); + } if (methodInfo.GetCustomAttributes(typeof(PostInjectAttribute), true).Length > 0) { + if (!methodInfo.IsPublic) + throw new Exception($"PostInject method must be public. {targetType.Name}.{methodInfo.Name}()"); if (methodInfo.GetParameters().Length > 0) throw new Exception("PostInject method should not have any parameters"); PostInjectMethods.Add(methodInfo); @@ -26,6 +32,8 @@ public TargetMethodsInfo(Type targetType) if (methodInfo.GetCustomAttributes(typeof(OnInjectedAttribute), true).Length > 0) { + if (!methodInfo.IsPublic) + throw new Exception($"OnInjected method must be public. {targetType.Name}.{methodInfo.Name}()"); if (methodInfo.GetParameters().Length > 0) throw new Exception("OnInjected method should not have any parameters"); OnInjectedMethods.Add(methodInfo); diff --git a/Runtime/TargetPropertiesInfo.cs b/Runtime/TargetPropertiesInfo.cs index cc77f8b..80567a1 100644 --- a/Runtime/TargetPropertiesInfo.cs +++ b/Runtime/TargetPropertiesInfo.cs @@ -10,10 +10,15 @@ internal class TargetPropertiesInfo public TargetPropertiesInfo(Type targetType) { - foreach (var propertyInfo in targetType.GetProperties()) + foreach (var propertyInfo in targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { - if (propertyInfo.GetCustomAttributes(typeof(InjectAttribute), true).Length > 0) - InjectProperties.Add(propertyInfo); + if (propertyInfo.GetCustomAttributes(typeof(InjectAttribute), true).Length <= 0) + continue; + + if (!propertyInfo.CanWrite || !propertyInfo.SetMethod.IsPublic) + throw new Exception($"Property must have a public setter. {targetType.Name}.{propertyInfo.Name}"); + + InjectProperties.Add(propertyInfo); } } diff --git a/Tests/Components/PropertyInjectionComponent.cs b/Tests/Components/PropertyInjectionComponent.cs index 24606d3..81f3c27 100644 --- a/Tests/Components/PropertyInjectionComponent.cs +++ b/Tests/Components/PropertyInjectionComponent.cs @@ -5,6 +5,6 @@ namespace Doinject.Tests public class PropertyInjectionComponent : MonoBehaviour, IInjectableComponent { [Inject] - public InjectedObject InjectedObject { get; private set; } + public InjectedObject InjectedObject { get; set; } } } \ No newline at end of file diff --git a/Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs b/Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs new file mode 100644 index 0000000..31bb822 --- /dev/null +++ b/Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs @@ -0,0 +1,10 @@ +using UnityEngine; + +namespace Doinject.Tests +{ + public class PropertyInjectionWithNonPublicSetterComponent : MonoBehaviour, IInjectableComponent + { + [Inject] + public InjectedObject InjectedObject { get; private set; } + } +} \ No newline at end of file diff --git a/Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs.meta b/Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs.meta new file mode 100644 index 0000000..ec73184 --- /dev/null +++ b/Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c9eebc0e77444858bd0b3c358ec68792 +timeCreated: 1708473725 \ No newline at end of file diff --git a/Tests/InjectionTest.cs b/Tests/InjectionTest.cs index 4683819..e937392 100644 --- a/Tests/InjectionTest.cs +++ b/Tests/InjectionTest.cs @@ -144,5 +144,22 @@ public async Task PropertyInjectionComponentTest() var instance = await container.ResolveAsync(); Assert.NotNull(instance.InjectedObject); } + + [Test] + public async Task PropertyInjectionWithNonPublicSetterComponentTest() + { + container.BindTransient(); + container.BindTransient(); + try + { + await container.ResolveAsync(); + } + catch (Exception e) + { + Assert.Pass(); + + } + Assert.Fail(); + } } } \ No newline at end of file