[英]Contextual Binding in .NET Core 2.2 DI Container?
在 .NET 框架中,您可以使用 Ninject 來實現上下文綁定。
例如:
Bind<IWarrior>().To<Samurai>().WhenInjectedInto(typeof(OnLandAttack));
Bind<IWarrior>().To<SpecialNinja>().WhenInjectedInto(typeof(AmphibiousAttack));
我需要這樣的東西並最終為它創建了一個 NuGet 包: https ://www.nuget.org/packages/ServiceProviderContextualBinding/
用法如下所示:
services.AddSingleton<IService, DefaultService>();
services.AddSingleton<ReplacementService>();
services.WithReplacement<IService, ReplacementService>()
.AddSingleton<Consumer>();
這個包基本上是ActivatorUtilities.CreateInstance 方法的一個外觀,它是 MS DI 代碼的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.