Skip to content

Commit 49889cc

Browse files
authored
Fix Resolve Issue with Multiple AutoRegister Calls (#155)
1 parent 9a4b579 commit 49889cc

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/TinyIoC/TinyIoC.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,7 +3582,7 @@ where localType.IsAssignableFrom(implementationType)
35823582
}
35833583

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

36053605
return false;
36063606
}
3607-
3607+
36083608
// TODO - find a better way to remove "system" types from the auto registration
3609-
private readonly List<Func<Type, bool>> ignoreChecks = new List<Func<Type, bool>>()
3609+
private static readonly IReadOnlyList<Func<Type, bool>> ignoreChecks = new List<Func<Type, bool>>()
36103610
{
36113611
t => t.FullName.StartsWith("System.", StringComparison.Ordinal),
36123612
t => t.FullName.StartsWith("Microsoft.", StringComparison.Ordinal),
@@ -3619,19 +3619,10 @@ private bool IsIgnoredAssembly(Assembly assembly)
36193619

36203620
private bool IsIgnoredType(Type type, Func<Type, bool> registrationPredicate)
36213621
{
3622-
if (registrationPredicate != null && !registrationPredicate(type))
3623-
{
3624-
ignoreChecks.Add(t => !registrationPredicate(t));
3622+
if (ignoreChecks.Any(c => c(type)))
36253623
return true;
3626-
}
36273624

3628-
for (int i = 0; i < ignoreChecks.Count; i++)
3629-
{
3630-
if (ignoreChecks[i](type))
3631-
return true;
3632-
}
3633-
3634-
return false;
3625+
return registrationPredicate != null && !registrationPredicate(type);
36353626
}
36363627

36373628
private void RegisterDefaultTypes()

tests/TinyIoC.Tests/TinyIoCTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,6 +3393,20 @@ public void AutoRegister_TypeExcludedViaPredicate_FailsToResolveType()
33933393
//Assert.IsInstanceOfType(result, typeof(TinyIoCResolutionException));
33943394
}
33953395

3396+
[TestMethod]
3397+
public void AutoRegister_Resolve_MultipleCalls()
3398+
{
3399+
var container = UtilityMethods.GetContainer();
3400+
3401+
container.AutoRegister(new[] { this.GetType().Assembly }, t => typeof(ITestInterface).IsAssignableFrom(t));
3402+
Assert.IsNotNull(container.Resolve<ITestInterface>());
3403+
AssertHelper.ThrowsException<TinyIoCResolutionException>(() => container.Resolve<ITestInterface2>());
3404+
3405+
container.AutoRegister(new[] { this.GetType().Assembly }, t => typeof(ITestInterface2).IsAssignableFrom(t));
3406+
Assert.IsNotNull(container.Resolve<ITestInterface>());
3407+
Assert.IsNotNull(container.Resolve<ITestInterface2>());
3408+
}
3409+
33963410
#if RESOLVE_OPEN_GENERICS
33973411
[TestMethod]
33983412
public void Register_OpenGeneric_DoesNotThrow()

0 commit comments

Comments
 (0)