Skip to content

Commit 574bdc6

Browse files
IpashilovoMathijs-Bakker
authored andcommitted
ResolveAll generics with InjectSources
Opportunity to use something like ResolveAll<IDisposable>() with InjectSource ResolveAll generics with InjectSources Opportunity to use something like ResolveAll<IDisposable>() with InjectSource Test
1 parent 0649848 commit 574bdc6

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

UnityProject/Assets/Plugins/Zenject/Source/Runtime/Main/DiContainer.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,11 +2469,21 @@ public List<TContract> ResolveAll<TContract>()
24692469
{
24702470
return (List<TContract>)ResolveAll(typeof(TContract));
24712471
}
2472+
2473+
public List<TContract> ResolveAll<TContract>(InjectSources injectSources)
2474+
{
2475+
return (List<TContract>)ResolveAll(typeof(TContract), injectSources);
2476+
}
24722477

24732478
public IList ResolveAll(Type contractType)
24742479
{
24752480
return ResolveIdAll(contractType, null);
24762481
}
2482+
2483+
public IList ResolveAll(Type contractType, InjectSources injectSources)
2484+
{
2485+
return ResolveIdAll(contractType, null, injectSources);
2486+
}
24772487

24782488
public List<TContract> ResolveIdAll<TContract>(object identifier)
24792489
{
@@ -2489,6 +2499,17 @@ public IList ResolveIdAll(Type contractType, object identifier)
24892499
return ResolveAll(context);
24902500
}
24912501
}
2502+
2503+
public IList ResolveIdAll(Type contractType, object identifier, InjectSources injectSources)
2504+
{
2505+
using (var context = ZenPools.SpawnInjectContext(this, contractType))
2506+
{
2507+
context.Identifier = identifier;
2508+
context.Optional = true;
2509+
context.SourceType = injectSources;
2510+
return ResolveAll(context);
2511+
}
2512+
}
24922513

24932514
// Removes all bindings
24942515
public void UnbindAll()

UnityProject/Assets/Plugins/Zenject/Tests/UnitTests/Editor/Bindings/TestFromResolve.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ public void TestInjectSource1()
172172

173173
Assert.IsEqual(subContainer.Resolve<IFoo>(), foo1);
174174
}
175+
176+
[Test]
177+
public void TestResolveAllWithInjectSource()
178+
{
179+
var foo = new Foo();
180+
var secondFoo = new SecondFoo();
181+
182+
Container.BindInterfacesTo<Foo>().FromInstance(foo);
183+
184+
var subContainer = Container.CreateSubContainer();
185+
subContainer.BindInterfacesTo<SecondFoo>().FromInstance(secondFoo);
186+
187+
Assert.IsEqual(subContainer.ResolveAll<IFoo>(InjectSources.Any).Count, 2);
188+
Assert.IsEqual(subContainer.ResolveAll<IFoo>(InjectSources.Local).Count, 1);
189+
}
190+
175191

176192
[Test]
177193
public void TestInjectSource2()
@@ -203,5 +219,9 @@ interface IFoo
203219
class Foo : IFoo, IBar
204220
{
205221
}
222+
223+
class SecondFoo : IFoo
224+
{
225+
}
206226
}
207227
}

0 commit comments

Comments
 (0)