From 53163db8b5cf4452642c9bdc7306f8b5d7239e1c Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 27 Nov 2023 10:11:39 +0800 Subject: [PATCH] Update Dependency-Injection.md --- docs/en/Dependency-Injection.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/en/Dependency-Injection.md b/docs/en/Dependency-Injection.md index d8afe5c21c..51a395e3ea 100644 --- a/docs/en/Dependency-Injection.md +++ b/docs/en/Dependency-Injection.md @@ -269,6 +269,21 @@ public class MyService : ITransientDependency } ```` +#### IInjectPropertiesService + +You can use the `IInjectPropertiesService` service to inject properties of an object. Generally, it is a service outside of DI, such as manually created services. + +````C# +var injectPropertiesService = serviceProvider.GetRequiredService(); +var instance = new TestService(); + +// Set any properties on instance that can be resolved by IServiceProvider. +injectPropertiesService.InjectProperties(instance); + +// Set any null-valued properties on instance that can be resolved by the IServiceProvider. +injectPropertiesService.InjectUnsetProperties(instance); +```` + ### Resolve Service from IServiceProvider You may want to resolve a service directly from ``IServiceProvider``. In that case, you can inject `IServiceProvider` into your class and use the ``GetService`` or the `GetRequiredService` method as shown below: