未验证 提交 8db080e3 编写于 作者: T Tamás Turnyánszki 提交者: GitHub

Unable to bind Dictionary with key type "Enum" when key string case is different (#71926)

Use ignoreCase=true when parsing enum values.

Fix #71185
上级 11f162ae
......@@ -576,9 +576,10 @@ private static bool CanBindToTheseConstructorParameters(ParameterInfo[] construc
{
try
{
object key = keyTypeIsEnum ? Enum.Parse(keyType, child.Key) :
object key = keyTypeIsEnum ? Enum.Parse(keyType, child.Key, true) :
keyTypeIsInteger ? Convert.ChangeType(child.Key, keyType) :
child.Key;
var valueBindingPoint = new BindingPoint(
initialValueProvider: () =>
{
......
......@@ -491,6 +491,33 @@ public class ByteArrayOptions
public byte[] MyByteArray { get; set; }
}
public enum TestSettingsEnum
{
Option1,
Option2,
}
[Fact]
public void EnumBindCaseInsensitiveNotThrows()
{
var dic = new Dictionary<string, string>
{
{"Section:Option1", "opt1"},
{"Section:option2", "opt2"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();
var configSection = config.GetSection("Section");
var configOptions = new Dictionary<TestSettingsEnum, string>();
configSection.Bind(configOptions);
Assert.Equal("opt1", configOptions[TestSettingsEnum.Option1]);
Assert.Equal("opt2", configOptions[TestSettingsEnum.Option2]);
}
[Fact]
public void CanBindIConfigurationSection()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册