Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)) };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space here... To be removed

constructorArguments[i] = Expression.Call(GetServiceInfo, parameterTypeExpression);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.IWeatherProvider, TestEnums_Types.OpenWeatherMapWeatherProvider>(TestEnums_Types.WeatherProvider.OpenWeatherMap);
services.AddKeyedScoped<TestEnums_Types.IWeatherProvider, TestEnums_Types.AccuWeatherWeatherProvider>(TestEnums_Types.WeatherProvider.AccuWeather);
using var provider = services.BuildServiceProvider();

ObjectFactory<TestEnums_Types.WeatherController1> factory1 = ActivatorUtilities.CreateFactory<TestEnums_Types.WeatherController1>(Type.EmptyTypes);
TestEnums_Types.WeatherController1 value1 = factory1(provider, null);
Assert.Equal("AccuWeather", value1.DefaultWeatherProvider.GetWeatherForCity(""));

ObjectFactory<TestEnums_Types.WeatherController2> factory2 = ActivatorUtilities.CreateFactory<TestEnums_Types.WeatherController2>(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
Expand Down