From 332e42395ba34c3d4358ccca7d90e85024a27af1 Mon Sep 17 00:00:00 2001 From: AlexKuai Date: Tue, 6 Dec 2022 00:17:00 +0800 Subject: [PATCH] CallSiteRuntimeResolver. VisitFactory method implements adding IsInstanceOfType check --- .../src/ServiceLookup/CallSiteRuntimeResolver.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteRuntimeResolver.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteRuntimeResolver.cs index f41e18238bcf6..993ef9ce92b1a 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteRuntimeResolver.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteRuntimeResolver.cs @@ -174,9 +174,15 @@ protected override object VisitIEnumerable(IEnumerableCallSite enumerableCallSit return array; } - protected override object VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) + protected override object? VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) { - return factoryCallSite.Factory(context.Scope); + object? value = factoryCallSite.Factory(context.Scope); + Type serviceType = factoryCallSite.ServiceType; + if (value != null && !serviceType.IsInstanceOfType(value)) + { + throw new ArgumentException(SR.Format(SR.ConstantCantBeConvertedToServiceType, value.GetType(), serviceType)); + } + return value; } }