Skip to content

Commit

Permalink
Fix Resolve Issue with Multiple AutoRegister Calls (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
gillima authored Jan 27, 2022
1 parent 9a4b579 commit 49889cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/TinyIoC/TinyIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3582,7 +3582,7 @@ where localType.IsAssignableFrom(implementationType)
}

// TODO - find a better way to remove "system" assemblies from the auto registration
private readonly List<Func<Assembly, bool>> ignoredAssemlies = new List<Func<Assembly, bool>>()
private static readonly IReadOnlyList<Func<Assembly, bool>> ignoredAssemlies = new List<Func<Assembly, bool>>()
{
asm => asm.FullName.StartsWith("Microsoft.", StringComparison.Ordinal),
asm => asm.FullName.StartsWith("System.", StringComparison.Ordinal),
Expand All @@ -3604,9 +3604,9 @@ private bool IsIgnoredAssembly(Assembly assembly)

return false;
}

// TODO - find a better way to remove "system" types from the auto registration
private readonly List<Func<Type, bool>> ignoreChecks = new List<Func<Type, bool>>()
private static readonly IReadOnlyList<Func<Type, bool>> ignoreChecks = new List<Func<Type, bool>>()
{
t => t.FullName.StartsWith("System.", StringComparison.Ordinal),
t => t.FullName.StartsWith("Microsoft.", StringComparison.Ordinal),
Expand All @@ -3619,19 +3619,10 @@ private bool IsIgnoredAssembly(Assembly assembly)

private bool IsIgnoredType(Type type, Func<Type, bool> registrationPredicate)
{
if (registrationPredicate != null && !registrationPredicate(type))
{
ignoreChecks.Add(t => !registrationPredicate(t));
if (ignoreChecks.Any(c => c(type)))
return true;
}

for (int i = 0; i < ignoreChecks.Count; i++)
{
if (ignoreChecks[i](type))
return true;
}

return false;
return registrationPredicate != null && !registrationPredicate(type);
}

private void RegisterDefaultTypes()
Expand Down
14 changes: 14 additions & 0 deletions tests/TinyIoC.Tests/TinyIoCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3393,6 +3393,20 @@ public void AutoRegister_TypeExcludedViaPredicate_FailsToResolveType()
//Assert.IsInstanceOfType(result, typeof(TinyIoCResolutionException));
}

[TestMethod]
public void AutoRegister_Resolve_MultipleCalls()
{
var container = UtilityMethods.GetContainer();

container.AutoRegister(new[] { this.GetType().Assembly }, t => typeof(ITestInterface).IsAssignableFrom(t));
Assert.IsNotNull(container.Resolve<ITestInterface>());
AssertHelper.ThrowsException<TinyIoCResolutionException>(() => container.Resolve<ITestInterface2>());

container.AutoRegister(new[] { this.GetType().Assembly }, t => typeof(ITestInterface2).IsAssignableFrom(t));
Assert.IsNotNull(container.Resolve<ITestInterface>());
Assert.IsNotNull(container.Resolve<ITestInterface2>());
}

#if RESOLVE_OPEN_GENERICS
[TestMethod]
public void Register_OpenGeneric_DoesNotThrow()
Expand Down

0 comments on commit 49889cc

Please sign in to comment.