Skip to content

Commit 3322d6b

Browse files
committed
resolved #82
1 parent 4364522 commit 3322d6b

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/CatLib.Core.Tests/Support/Container/ContainerTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,34 @@ public void TestExtendGivenMismatchedType()
25902590
container.Extend<IContainer>((instance) => "123");
25912591
container.Make<TestExtendGivenMismatchedTypeClass>();
25922592
}
2593+
2594+
[TestMethod]
2595+
public void TestThisSet()
2596+
{
2597+
var container = new Container();
2598+
container["hello"] = "world";
2599+
Assert.AreEqual("world", container.Make("hello"));
2600+
}
2601+
2602+
[TestMethod]
2603+
public void TestMultThisSet()
2604+
{
2605+
var container = new Container();
2606+
container["hello"] = "world";
2607+
container["world"] = "hello";
2608+
Assert.AreEqual("world", container.Make("hello"));
2609+
Assert.AreEqual("hello", container.Make("world"));
2610+
}
2611+
2612+
[TestMethod]
2613+
public void TestExistsThisSet()
2614+
{
2615+
var container = new Container();
2616+
container["hello"] = "world";
2617+
Assert.AreEqual("world", container.Make("hello"));
2618+
container["hello"] = 123;
2619+
Assert.AreEqual(123, container.Make("hello"));
2620+
}
25932621
#endregion
25942622

25952623
/// <summary>

src/CatLib.Core/Support/Container/Container.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,22 @@ public object Make(string service, params object[] userParams)
562562
/// </summary>
563563
/// <param name="service">服务名或者别名</param>
564564
/// <returns>服务实例,如果构造失败那么返回null</returns>
565-
public object this[string service] => Make(service);
565+
public object this[string service]
566+
{
567+
get => Make(service);
568+
set
569+
{
570+
lock (syncRoot)
571+
{
572+
var bind = GetBind(service);
573+
if (bind != null)
574+
{
575+
Unbind(bind);
576+
}
577+
Bind(service, (_, __) => value, false);
578+
}
579+
}
580+
}
566581

567582
/// <summary>
568583
/// 获取一个回调,当执行回调可以生成指定的服务
@@ -899,6 +914,7 @@ internal void Unbind(IBindable bindable)
899914
{
900915
lock (syncRoot)
901916
{
917+
GuardFlushing();
902918
Release(bindable.Service);
903919
if (aliasesReverse.TryGetValue(bindable.Service, out List<string> serviceList))
904920
{

src/CatLib.Core/Support/Container/IContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public interface IContainer
196196
/// </summary>
197197
/// <param name="service">服务名或者别名</param>
198198
/// <returns>服务实例,如果构造失败那么返回null</returns>
199-
object this[string service] { get; }
199+
object this[string service] { get; set; }
200200

201201
/// <summary>
202202
/// 获取一个回调,当执行回调可以生成指定的服务

0 commit comments

Comments
 (0)