diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs index 6d8a30b6dd5d4e..0e7bd8a0c4282c 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs @@ -418,7 +418,8 @@ private static BlockExpression BuildFactoryExpression( Expression.Constant(parameterType, typeof(Type)), Expression.Constant(constructor.DeclaringType, typeof(Type)), Expression.Constant(hasDefaultValue), - Expression.Constant(keyAttribute?.Key) }; + Expression.Convert(Expression.Constant(keyAttribute?.Key), typeof(object)) }; + constructorArguments[i] = Expression.Call(GetServiceInfo, parameterTypeExpression); } diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ActivatorUtilitiesTests.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ActivatorUtilitiesTests.cs index 8e0d07c0b0acd4..b6e41088e203ec 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ActivatorUtilitiesTests.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ActivatorUtilitiesTests.cs @@ -361,6 +361,67 @@ public void CreateFactory_CreatesFactoryMethod_KeyedParams(bool useDynamicCode) }, options); } + [Fact] + public void CreateFactory_CreatesFactoryMethod_KeyedParams_EnumType() + { + var services = new ServiceCollection(); + services.AddKeyedScoped(TestEnums_Types.WeatherProvider.OpenWeatherMap); + services.AddKeyedScoped(TestEnums_Types.WeatherProvider.AccuWeather); + using var provider = services.BuildServiceProvider(); + + ObjectFactory factory1 = ActivatorUtilities.CreateFactory(Type.EmptyTypes); + TestEnums_Types.WeatherController1 value1 = factory1(provider, null); + Assert.Equal("AccuWeather", value1.DefaultWeatherProvider.GetWeatherForCity("")); + + ObjectFactory factory2 = ActivatorUtilities.CreateFactory(Type.EmptyTypes); + TestEnums_Types.WeatherController2 value2 = factory2(provider, null); + Assert.Equal("OpenWeather", value2.DefaultWeatherProvider.GetWeatherForCity("")); + } + + private class TestEnums_Types + { + public enum WeatherProvider + { + AccuWeather = 1, + OpenWeatherMap = 2, + } + + public interface IWeatherProvider + { + string GetWeatherForCity(string city); + } + + public sealed class AccuWeatherWeatherProvider : IWeatherProvider + { + public string GetWeatherForCity(string city) => "AccuWeather"; + } + + public sealed class OpenWeatherMapWeatherProvider : IWeatherProvider + { + public string GetWeatherForCity(string city) => "OpenWeather"; + } + + public sealed class WeatherController1 + { + public IWeatherProvider DefaultWeatherProvider { get; } + + public WeatherController1([FromKeyedServices(WeatherProvider.AccuWeather)] IWeatherProvider defaultWeatherProvider) + { + DefaultWeatherProvider = defaultWeatherProvider; + } + } + + public sealed class WeatherController2 + { + public IWeatherProvider DefaultWeatherProvider { get; } + + public WeatherController2([FromKeyedServices(WeatherProvider.OpenWeatherMap)] IWeatherProvider defaultWeatherProvider) + { + DefaultWeatherProvider = defaultWeatherProvider; + } + } + } + [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] [InlineData(true)] #if NET