Skip to content

Commit

Permalink
Injection point must be public
Browse files Browse the repository at this point in the history
  • Loading branch information
mewlist committed Feb 21, 2024
1 parent 672e3aa commit fb562d5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Runtime/TargetMethodsInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@ 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);
}

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);
Expand Down
11 changes: 8 additions & 3 deletions Runtime/TargetPropertiesInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Components/PropertyInjectionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Doinject.Tests
public class PropertyInjectionComponent : MonoBehaviour, IInjectableComponent
{
[Inject]
public InjectedObject InjectedObject { get; private set; }
public InjectedObject InjectedObject { get; set; }
}
}
10 changes: 10 additions & 0 deletions Tests/Components/PropertyInjectionWithNonPublicSetterComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityEngine;

namespace Doinject.Tests
{
public class PropertyInjectionWithNonPublicSetterComponent : MonoBehaviour, IInjectableComponent
{
[Inject]
public InjectedObject InjectedObject { get; private set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Tests/InjectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,22 @@ public async Task PropertyInjectionComponentTest()
var instance = await container.ResolveAsync<PropertyInjectionComponent>();
Assert.NotNull(instance.InjectedObject);
}

[Test]
public async Task PropertyInjectionWithNonPublicSetterComponentTest()
{
container.BindTransient<PropertyInjectionWithNonPublicSetterComponent>();
container.BindTransient<InjectedObject>();
try
{
await container.ResolveAsync<PropertyInjectionWithNonPublicSetterComponent>();
}
catch (Exception e)
{
Assert.Pass();

}
Assert.Fail();
}
}
}

0 comments on commit fb562d5

Please sign in to comment.