Skip to content

Commit

Permalink
Merge pull request #37 from hadashiA/ku/fix-inject-constructors
Browse files Browse the repository at this point in the history
Fix a bug that inject constructor targets .cctor
  • Loading branch information
hadashiA authored Oct 17, 2020
2 parents 25c0fdf + 536b445 commit b0f7d1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public static InjectTypeInfo Analyze(Type type)
// Constructor, single [Inject] constructor -> single most parameters constuctor
var injectConstructorCount = 0;
var maxParameters = -1;
foreach (var constructorInfo in typeInfo.DeclaredConstructors)
{
foreach (var constructorInfo in typeInfo.GetConstructors(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
if (constructorInfo.IsDefined(typeof(InjectAttribute), true))
{
if (++injectConstructorCount > 1)
Expand Down
16 changes: 15 additions & 1 deletion VContainer/Assets/VContainer/Tests/TypeAnalyzerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public HasNoAttributeConstructor(int x, int y)
}
}

class HasStaticConstructor
{
static int Foo = 400;

[Inject]
public HasStaticConstructor()
{
}
}

class HasInjectConstructor
{
[Inject]
Expand Down Expand Up @@ -56,7 +66,11 @@ public void Analyze()
}
{
var injectTypeInfo = TypeAnalyzer.Analyze(typeof(HasInjectConstructor));
// Assert.That(injectTypeInfo.InjectConstructor.ConstructorInfo.GetCustomAttribute<InjectAttribute>(), Is.Not.Null);
Assert.That(injectTypeInfo.InjectConstructor.ConstructorInfo.GetCustomAttribute<InjectAttribute>(), Is.Not.Null);
}
{
var injectTypeInfo = TypeAnalyzer.Analyze(typeof(HasStaticConstructor));
Assert.That(injectTypeInfo.InjectConstructor.ConstructorInfo.IsStatic, Is.False);
}
}
}
Expand Down

0 comments on commit b0f7d1f

Please sign in to comment.