diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs index 9e6a7d326c9bd0..f13edb1872ebee 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs @@ -19,10 +19,6 @@ public static class ConfigurationBinder private const string TrimmingWarningMessage = "In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed."; private const string InstanceGetTypeTrimmingWarningMessage = "Cannot statically analyze the type of instance so its members may be trimmed"; private const string PropertyTrimmingWarningMessage = "Cannot statically analyze property.PropertyType so its members may be trimmed."; - private const string BindSingleElementsToArraySwitch = "Microsoft.Extensions.Configuration.BindSingleElementsToArray"; - - // Enable this switch by default. - private static bool ShouldBindSingleElementsToArray { get; } = AppContext.TryGetSwitch(BindSingleElementsToArraySwitch, out bool verifyCanBindSingleElementsToArray) ? verifyCanBindSingleElementsToArray : true; /// /// Attempts to bind the configuration instance to a new instance of type T. @@ -366,7 +362,7 @@ private static object BindInstance( return convertedValue; } - if (config != null && (config.GetChildren().Any() || (configValue != null && ShouldBindSingleElementsToArray))) + if (config != null && config.GetChildren().Any()) { // If we don't have an instance, try to create one if (instance == null) @@ -499,7 +495,7 @@ private static void BindCollection( Type itemType = collectionType.GenericTypeArguments[0]; MethodInfo addMethod = collectionType.GetMethod("Add", DeclaredOnlyLookup); - foreach (IConfigurationSection section in GetChildrenOrSelf(config)) + foreach (IConfigurationSection section in config.GetChildren()) { try { @@ -522,7 +518,7 @@ private static void BindCollection( [RequiresUnreferencedCode("Cannot statically analyze what the element type is of the Array so its members may be trimmed.")] private static Array BindArray(Array source, IConfiguration config, BinderOptions options) { - IConfigurationSection[] children = GetChildrenOrSelf(config).ToArray(); + IConfigurationSection[] children = config.GetChildren().ToArray(); int arrayLength = source.Length; Type elementType = source.GetType().GetElementType(); var newArray = Array.CreateInstance(elementType, arrayLength + children.Length); @@ -706,26 +702,5 @@ private static string GetPropertyName(MemberInfo property) return property.Name; } - - private static IEnumerable GetChildrenOrSelf(IConfiguration config) - { - if (!ShouldBindSingleElementsToArray) - { - return config.GetChildren(); - } - - IEnumerable children; - // If configuration's children is an array, the configuration key will be a number - if (config.GetChildren().Any(a => long.TryParse(a.Key, out _))) - { - children = config.GetChildren(); - } - else - { - children = new[] { config as IConfigurationSection }; - } - - return children; - } } -} \ No newline at end of file +} diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs index 1c9e89176a1321..c5dbd9d90821a2 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.ComponentModel; using System.Reflection; -using Microsoft.DotNet.RemoteExecutor; using Xunit; namespace Microsoft.Extensions.Configuration.Binder.Test @@ -937,59 +936,6 @@ public void ExceptionWhenTryingToBindToByteArray() exception.Message); } - [Fact] - public void CanBindSingleElementToCollection() - { - var dic = new Dictionary - { - {"MyString", "hello world"}, - {"Nested:Integer", "11"}, - }; - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddInMemoryCollection(dic); - IConfiguration config = configurationBuilder.Build(); - - var stringArr = config.GetSection("MyString").Get(); - Assert.Equal("hello world", stringArr[0]); - Assert.Equal(1, stringArr.Length); - - var stringAsStr = config.GetSection("MyString").Get(); - Assert.Equal("hello world", stringAsStr); - - var nested = config.GetSection("Nested").Get(); - Assert.Equal(11, nested.Integer); - - var nestedAsArray = config.GetSection("Nested").Get(); - Assert.Equal(11, nestedAsArray[0].Integer); - Assert.Equal(1, nestedAsArray.Length); - } - - [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] - public void CannotBindSingleElementToCollectionWhenSwitchSet() - { - RemoteExecutor.Invoke(() => - { - AppContext.SetSwitch("Microsoft.Extensions.Configuration.BindSingleElementsToArray", false); - - var dic = new Dictionary - { - {"MyString", "hello world"}, - {"Nested:Integer", "11"}, - }; - - var configurationBuilder = new ConfigurationBuilder(); - configurationBuilder.AddInMemoryCollection(dic); - IConfiguration config = configurationBuilder.Build(); - - var stringArr = config.GetSection("MyString").Get(); - Assert.Null(stringArr); - - var stringAsStr = config.GetSection("MyString").Get(); - Assert.Equal("hello world", stringAsStr); - }).Dispose(); - } - private interface ISomeInterface { } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Microsoft.Extensions.Configuration.Binder.Tests.csproj b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Microsoft.Extensions.Configuration.Binder.Tests.csproj index 6f72c650a34d4c..8fcc6f146609b7 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Microsoft.Extensions.Configuration.Binder.Tests.csproj +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Microsoft.Extensions.Configuration.Binder.Tests.csproj @@ -3,7 +3,6 @@ $(NetCoreAppCurrent);$(NetFrameworkMinimum) true - true