diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Emitter.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Emitter.cs
index 756e10bc26b8d..cb67c7ee1f5fb 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Emitter.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Emitter.cs
@@ -96,19 +96,19 @@ private void EmitBindCoreCall(
EmitBindCoreCall(memberAccessExpr, initKind);
}
- void EmitBindCoreCall(string objExpression, InitializationKind initKind)
+ void EmitBindCoreCall(string instanceExpr, InitializationKind initKind)
{
- string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {objExpression}, {Identifier.binderOptions});";
- EmitObjectInit(objExpression, initKind);
+ string bindCoreCall = $@"{nameof(MethodsToGen_CoreBindingHelper.BindCore)}({configArgExpr}, ref {instanceExpr}, {Identifier.binderOptions});";
+ EmitObjectInit(instanceExpr, initKind);
_writer.WriteLine(bindCoreCall);
- writeOnSuccess?.Invoke(objExpression);
+ writeOnSuccess?.Invoke(instanceExpr);
}
- void EmitObjectInit(string objExpression, InitializationKind initKind)
+ void EmitObjectInit(string instanceExpr, InitializationKind initKind)
{
if (initKind is not InitializationKind.None)
{
- this.EmitObjectInit(type, objExpression, initKind, configArgExpr);
+ this.EmitObjectInit(type, instanceExpr, initKind, configArgExpr);
}
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/ConfigurationBinder.cs
index 64064887c7c70..c7ead12d65589 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/ConfigurationBinder.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/ConfigurationBinder.cs
@@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
-using SourceGenerators;
namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
@@ -101,13 +100,13 @@ private void EmitBindMethods_ConfigurationBinder()
return;
}
- string objParamExpr = $"object? {Identifier.obj}";
+ string instanceParamExpr = $"object? {Identifier.instance}";
if (ShouldEmitMethods(MethodsToGen_ConfigurationBinder.Bind_instance))
{
EmitMethods(
MethodsToGen_ConfigurationBinder.Bind_instance,
- additionalParams: objParamExpr,
+ additionalParams: instanceParamExpr,
configExpression: Identifier.configuration,
configureOptions: false);
}
@@ -116,7 +115,7 @@ private void EmitBindMethods_ConfigurationBinder()
{
EmitMethods(
MethodsToGen_ConfigurationBinder.Bind_instance_BinderOptions,
- additionalParams: $"{objParamExpr}, {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions}",
+ additionalParams: $"{instanceParamExpr}, {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions}",
configExpression: Identifier.configuration,
configureOptions: true);
}
@@ -125,7 +124,7 @@ private void EmitBindMethods_ConfigurationBinder()
{
EmitMethods(
MethodsToGen_ConfigurationBinder.Bind_key_instance,
- additionalParams: $"string {Identifier.key}, {objParamExpr}",
+ additionalParams: $"string {Identifier.key}, {instanceParamExpr}",
configExpression: $"{Expression.configurationGetSection}({Identifier.key})",
configureOptions: false);
}
@@ -141,17 +140,17 @@ void EmitMethods(MethodsToGen_ConfigurationBinder method, string additionalParam
EmitInterceptsLocationAnnotations(interceptorInfoList);
EmitStartBlock($"public static void {Identifier.Bind}_{type.DisplayString.ToIdentifierSubstring()}(this {Identifier.IConfiguration} {Identifier.configuration}, {additionalParams})");
- if (!EmitInitException(type) && type.NeedsMemberBinding)
+ if (type.NeedsMemberBinding)
{
string binderOptionsArg = configureOptions ? $"{Identifier.GetBinderOptions}({Identifier.configureOptions})" : $"{Identifier.binderOptions}: null";
EmitCheckForNullArgument_WithBlankLine(Identifier.configuration);
if (!type.IsValueType)
{
- EmitCheckForNullArgument_WithBlankLine(Identifier.obj);
+ EmitCheckForNullArgument_WithBlankLine(Identifier.instance, voidReturn: true);
}
_writer.WriteLine($$"""
- var {{Identifier.typedObj}} = ({{type.EffectiveType.DisplayString}}){{Identifier.obj}};
+ var {{Identifier.typedObj}} = ({{type.EffectiveType.DisplayString}}){{Identifier.instance}};
{{nameof(MethodsToGen_CoreBindingHelper.BindCore)}}({{configExpression}}, ref {{Identifier.typedObj}}, {{binderOptionsArg}});
""");
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/CoreBindingHelpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/CoreBindingHelpers.cs
index f30408fad596d..bb2bd7ac61d15 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/CoreBindingHelpers.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/CoreBindingHelpers.cs
@@ -101,8 +101,8 @@ private void EmitGetCoreMethod()
}
else if (!EmitInitException(effectiveType))
{
- EmitBindCoreCall(effectiveType, Identifier.obj, Identifier.configuration, InitializationKind.Declaration);
- _writer.WriteLine($"return {Identifier.obj};");
+ EmitBindCoreCall(effectiveType, Identifier.instance, Identifier.configuration, InitializationKind.Declaration);
+ _writer.WriteLine($"return {Identifier.instance};");
}
EmitEndBlock();
@@ -168,9 +168,8 @@ private void EmitBindCoreMainMethod()
}
EmitBlankLineIfRequired();
- EmitStartBlock($"public static void {nameof(MethodsToGen_CoreBindingHelper.BindCoreMain)}({Identifier.IConfiguration} {Identifier.configuration}, object {Identifier.obj}, Type {Identifier.type}, {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions})");
- EmitCheckForNullArgument_WithBlankLine(Identifier.configuration);
- EmitCheckForNullArgument_WithBlankLine(Identifier.obj);
+ EmitStartBlock($"public static void {nameof(MethodsToGen_CoreBindingHelper.BindCoreMain)}({Identifier.IConfiguration} {Identifier.configuration}, object {Identifier.instance}, Type {Identifier.type}, {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions})");
+ EmitCheckForNullArgument_WithBlankLine(Identifier.instance, voidReturn: true);
EmitIConfigurationHasValueOrChildrenCheck(voidReturn: true);
_writer.WriteLine($"{Identifier.BinderOptions}? {Identifier.binderOptions} = {Identifier.GetBinderOptions}({Identifier.configureOptions});");
_writer.WriteLine();
@@ -184,7 +183,7 @@ private void EmitBindCoreMainMethod()
EmitStartBlock($"{conditionKindExpr} ({Identifier.type} == typeof({type.DisplayString}))");
if (!EmitInitException(effectiveType))
{
- _writer.WriteLine($"var {Identifier.temp} = ({effectiveType.DisplayString}){Identifier.obj};");
+ _writer.WriteLine($"var {Identifier.temp} = ({effectiveType.DisplayString}){Identifier.instance};");
EmitBindCoreCall(type, Identifier.temp, Identifier.configuration, InitializationKind.None);
_writer.WriteLine($"return;");
}
@@ -213,9 +212,7 @@ private void EmitBindCoreMethods()
private void EmitBindCoreMethod(TypeSpec type)
{
- Debug.Assert(type.CanInitialize);
-
- string objParameterExpression = $"ref {type.DisplayString} {Identifier.obj}";
+ string objParameterExpression = $"ref {type.DisplayString} {Identifier.instance}";
EmitStartBlock(@$"public static void {nameof(MethodsToGen_CoreBindingHelper.BindCore)}({Identifier.IConfiguration} {Identifier.configuration}, {objParameterExpression}, {Identifier.BinderOptions}? {Identifier.binderOptions})");
TypeSpec effectiveType = type.EffectiveType;
@@ -618,15 +615,15 @@ private void EmitPopulationImplForArray(EnumerableSpec type)
// Resize array and add binded elements.
_writer.WriteLine($$"""
- {{Identifier.Int32}} {{Identifier.originalCount}} = {{Identifier.obj}}.{{Identifier.Length}};
- {{Identifier.Array}}.{{Identifier.Resize}}(ref {{Identifier.obj}}, {{Identifier.originalCount}} + {{tempIdentifier}}.{{Identifier.Count}});
- {{tempIdentifier}}.{{Identifier.CopyTo}}({{Identifier.obj}}, {{Identifier.originalCount}});
+ {{Identifier.Int32}} {{Identifier.originalCount}} = {{Identifier.instance}}.{{Identifier.Length}};
+ {{Identifier.Array}}.{{Identifier.Resize}}(ref {{Identifier.instance}}, {{Identifier.originalCount}} + {{tempIdentifier}}.{{Identifier.Count}});
+ {{tempIdentifier}}.{{Identifier.CopyTo}}({{Identifier.instance}}, {{Identifier.originalCount}});
""");
}
private void EmitPopulationImplForEnumerableWithAdd(EnumerableSpec type)
{
- EmitCollectionCastIfRequired(type, out string objIdentifier);
+ EmitCollectionCastIfRequired(type, out string instanceIdentifier);
Emit_Foreach_Section_In_ConfigChildren_StartBlock();
@@ -638,14 +635,14 @@ private void EmitPopulationImplForEnumerableWithAdd(EnumerableSpec type)
stringParsableType,
Expression.sectionValue,
Expression.sectionPath,
- (parsedValueExpr) => _writer.WriteLine($"{objIdentifier}.{Identifier.Add}({parsedValueExpr});"),
+ (parsedValueExpr) => _writer.WriteLine($"{instanceIdentifier}.{Identifier.Add}({parsedValueExpr});"),
checkForNullSectionValue: true,
useIncrementalStringValueIdentifier: false);
}
else
{
EmitBindCoreCall(elementType, Identifier.value, Identifier.section, InitializationKind.Declaration);
- _writer.WriteLine($"{objIdentifier}.{Identifier.Add}({Identifier.value});");
+ _writer.WriteLine($"{instanceIdentifier}.{Identifier.Add}({Identifier.value});");
}
EmitEndBlock();
@@ -653,7 +650,7 @@ private void EmitPopulationImplForEnumerableWithAdd(EnumerableSpec type)
private void EmitBindCoreImplForDictionary(DictionarySpec type)
{
- EmitCollectionCastIfRequired(type, out string objIdentifier);
+ EmitCollectionCastIfRequired(type, out string instanceIdentifier);
Emit_Foreach_Section_In_ConfigChildren_StartBlock();
@@ -677,7 +674,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr)
stringParsableElementType,
Expression.sectionValue,
Expression.sectionPath,
- writeOnSuccess: parsedValueExpr => _writer.WriteLine($"{objIdentifier}[{parsedKeyExpr}] = {parsedValueExpr};"),
+ writeOnSuccess: parsedValueExpr => _writer.WriteLine($"{instanceIdentifier}[{parsedKeyExpr}] = {parsedValueExpr};"),
checkForNullSectionValue: true,
useIncrementalStringValueIdentifier: false);
}
@@ -696,7 +693,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr)
string expressionForElementIsNotNull = $"{Identifier.element} is not null";
string elementTypeDisplayString = elementType.DisplayString + (elementType.IsValueType ? string.Empty : "?");
- string expressionForElementExists = $"{objIdentifier}.{Identifier.TryGetValue}({parsedKeyExpr}, out {elementTypeDisplayString} {Identifier.element})";
+ string expressionForElementExists = $"{instanceIdentifier}.{Identifier.TryGetValue}({parsedKeyExpr}, out {elementTypeDisplayString} {Identifier.element})";
string conditionToUseExistingElement = expressionForElementExists;
// If key already exists, bind to existing element instance if not null (for ref types).
@@ -727,7 +724,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr)
}
EmitBindCoreCall(elementType, Identifier.element, Identifier.section, InitializationKind.None);
- _writer.WriteLine($"{objIdentifier}[{parsedKeyExpr}] = {Identifier.element};");
+ _writer.WriteLine($"{instanceIdentifier}[{parsedKeyExpr}] = {Identifier.element};");
}
}
@@ -747,7 +744,7 @@ private void EmitBindCoreImplForObject(ObjectSpec type)
bool noSetter_And_IsReadonly = !property.CanSet && property.Type is CollectionSpec { InitializationStrategy: InitializationStrategy.ParameterizedConstructor };
if (property.ShouldBind() && !noSetter_And_IsReadonly)
{
- string containingTypeRef = property.IsStatic ? type.DisplayString : Identifier.obj;
+ string containingTypeRef = property.IsStatic ? type.DisplayString : Identifier.instance;
EmitBindImplForMember(
property,
memberAccessExpr: $"{containingTypeRef}.{property.Name}",
@@ -883,14 +880,14 @@ private void EmitBindCoreCallForMember(
writeOnSuccess);
}
- private void EmitCollectionCastIfRequired(CollectionSpec type, out string objIdentifier)
+ private void EmitCollectionCastIfRequired(CollectionSpec type, out string instanceIdentifier)
{
- objIdentifier = Identifier.obj;
+ instanceIdentifier = Identifier.instance;
if (type.PopulationStrategy is CollectionPopulationStrategy.Cast_Then_Add)
{
- objIdentifier = Identifier.temp;
+ instanceIdentifier = Identifier.temp;
_writer.WriteLine($$"""
- if ({{Identifier.obj}} is not {{type.PopulationCastType!.DisplayString}} {{objIdentifier}})
+ if ({{Identifier.instance}} is not {{type.PopulationCastType!.DisplayString}} {{instanceIdentifier}})
{
return;
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/Helpers.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/Helpers.cs
index bad56b7ce3275..909272c767844 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/Helpers.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/Helpers.cs
@@ -45,6 +45,8 @@ private static class TypeDisplayString
private static class Identifier
{
public const string binderOptions = nameof(binderOptions);
+ public const string config = nameof(config);
+ public const string configureBinder = nameof(configureBinder);
public const string configureOptions = nameof(configureOptions);
public const string configuration = nameof(configuration);
public const string configSectionPath = nameof(configSectionPath);
@@ -55,7 +57,7 @@ private static class Identifier
public const string getPath = nameof(getPath);
public const string key = nameof(key);
public const string name = nameof(name);
- public const string obj = nameof(obj);
+ public const string instance = nameof(instance);
public const string optionsBuilder = nameof(optionsBuilder);
public const string originalCount = nameof(originalCount);
public const string section = nameof(section);
@@ -211,12 +213,16 @@ private void EmitBlankLineIfRequired()
_emitBlankLineBeforeNextStatement = true;
}
- private void EmitCheckForNullArgument_WithBlankLine(string paramName)
+ private void EmitCheckForNullArgument_WithBlankLine(string paramName, bool voidReturn = false)
{
+ string returnExpr = voidReturn
+ ? "return"
+ : $"throw new ArgumentNullException(nameof({paramName}))";
+
_writer.WriteLine($$"""
if ({{paramName}} is null)
{
- throw new ArgumentNullException(nameof({{paramName}}));
+ {{returnExpr}};
}
""");
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsBuilderConfigurationExtensions.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsBuilderConfigurationExtensions.cs
index d49198196fd49..7fd5d695eaf45 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsBuilderConfigurationExtensions.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsBuilderConfigurationExtensions.cs
@@ -30,25 +30,25 @@ private void EmitBindMethods_Extensions_OptionsBuilder()
}
const string documentation = @"/// Registers a configuration instance which will bind against.";
- const string paramList = $"{Identifier.IConfiguration} {Identifier.configuration}";
+ const string paramList = $"{Identifier.IConfiguration} {Identifier.config}";
if (ShouldEmitMethods(MethodsToGen_Extensions_OptionsBuilder.Bind_T))
{
EmitMethodStartBlock(MethodsToGen_Extensions_OptionsBuilder.Bind_T, "Bind", paramList, documentation);
- _writer.WriteLine($"return Bind({Identifier.optionsBuilder}, {Identifier.configuration}, {Identifier.configureOptions}: null);");
+ _writer.WriteLine($"return Bind({Identifier.optionsBuilder}, {Identifier.config}, {Identifier.configureBinder}: null);");
EmitEndBlock();
}
EmitMethodStartBlock(
MethodsToGen_Extensions_OptionsBuilder.Bind_T_BinderOptions,
"Bind",
- paramList + $", {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions}",
+ paramList + $", {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureBinder}",
documentation);
EmitCheckForNullArgument_WithBlankLine(Identifier.optionsBuilder);
_writer.WriteLine($$"""
- {{Identifier.Configure}}<{{Identifier.TOptions}}>({{Identifier.optionsBuilder}}.{{Identifier.Services}}, {{Identifier.optionsBuilder}}.Name, {{Identifier.configuration}}, {{Identifier.configureOptions}});
+ {{Identifier.Configure}}<{{Identifier.TOptions}}>({{Identifier.optionsBuilder}}.{{Identifier.Services}}, {{Identifier.optionsBuilder}}.Name, {{Identifier.config}}, {{Identifier.configureBinder}});
return {{Identifier.optionsBuilder}};
""");
@@ -63,19 +63,18 @@ private void EmitBindConfigurationMethod()
}
const string documentation = $@"/// Registers the dependency injection container to bind against the obtained from the DI service provider.";
- string paramList = $"string {Identifier.configSectionPath}, {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions} = null";
+ string paramList = $"string {Identifier.configSectionPath}, {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureBinder} = null";
EmitMethodStartBlock(MethodsToGen_Extensions_OptionsBuilder.BindConfiguration, "BindConfiguration", paramList, documentation);
EmitCheckForNullArgument_WithBlankLine(Identifier.optionsBuilder);
EmitCheckForNullArgument_WithBlankLine(Identifier.configSectionPath);
- EmitStartBlock($"{Identifier.optionsBuilder}.{Identifier.Configure}<{Identifier.IConfiguration}>(({Identifier.obj}, {Identifier.configuration}) =>");
- EmitCheckForNullArgument_WithBlankLine(Identifier.obj);
- EmitCheckForNullArgument_WithBlankLine(Identifier.configuration);
+ EmitStartBlock($"{Identifier.optionsBuilder}.{Identifier.Configure}<{Identifier.IConfiguration}>(({Identifier.instance}, {Identifier.config}) =>");
+ EmitCheckForNullArgument_WithBlankLine(Identifier.config);
_writer.WriteLine($$"""
- {{Identifier.IConfiguration}} {{Identifier.section}} = string.Equals(string.Empty, {{Identifier.configSectionPath}}, StringComparison.OrdinalIgnoreCase) ? {{Identifier.configuration}} : {{Identifier.configuration}}.{{Identifier.GetSection}}({{Identifier.configSectionPath}});
- {{nameof(MethodsToGen_CoreBindingHelper.BindCoreMain)}}({{Identifier.section}}, {{Identifier.obj}}, typeof({{Identifier.TOptions}}), {{Identifier.configureOptions}});
+ {{Identifier.IConfiguration}} {{Identifier.section}} = string.Equals(string.Empty, {{Identifier.configSectionPath}}, StringComparison.OrdinalIgnoreCase) ? {{Identifier.config}} : {{Identifier.config}}.{{Identifier.GetSection}}({{Identifier.configSectionPath}});
+ {{nameof(MethodsToGen_CoreBindingHelper.BindCoreMain)}}({{Identifier.section}}, {{Identifier.instance}}, typeof({{Identifier.TOptions}}), {{Identifier.configureBinder}});
""");
EmitEndBlock(endBraceTrailingSource: ");");
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsConfigurationServiceCollectionExtensions.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsConfigurationServiceCollectionExtensions.cs
index 41eeb8b657342..7577e0c49de4d 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsConfigurationServiceCollectionExtensions.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/OptionsConfigurationServiceCollectionExtensions.cs
@@ -24,12 +24,12 @@ private void EmitBindingExtensions_IServiceCollection()
private void EmitConfigureMethods()
{
const string defaultNameExpr = "string.Empty";
- string configParam = $"{Identifier.IConfiguration} {Identifier.configuration}";
+ string configParam = $"{Identifier.IConfiguration} {Identifier.config}";
if (ShouldEmitMethods(MethodsToGen_Extensions_ServiceCollection.Configure_T))
{
EmitStartMethod(MethodsToGen_Extensions_ServiceCollection.Configure_T, configParam);
- _writer.WriteLine($"return {Identifier.Configure}<{Identifier.TOptions}>({Identifier.services}, {defaultNameExpr}, {Identifier.configuration}, {Identifier.configureOptions}: null);");
+ _writer.WriteLine($"return {Identifier.Configure}<{Identifier.TOptions}>({Identifier.services}, {defaultNameExpr}, {Identifier.config}, {Identifier.configureOptions}: null);");
EmitEndBlock();
}
@@ -38,7 +38,7 @@ private void EmitConfigureMethods()
EmitStartMethod(
MethodsToGen_Extensions_ServiceCollection.Configure_T_name,
paramList: $"string? {Identifier.name}, " + configParam);
- _writer.WriteLine($"return {Identifier.Configure}<{Identifier.TOptions}>({Identifier.services}, {Identifier.name}, {Identifier.configuration}, {Identifier.configureOptions}: null);");
+ _writer.WriteLine($"return {Identifier.Configure}<{Identifier.TOptions}>({Identifier.services}, {Identifier.name}, {Identifier.config}, {Identifier.configureOptions}: null);");
EmitEndBlock();
}
@@ -47,22 +47,20 @@ private void EmitConfigureMethods()
EmitStartMethod(
MethodsToGen_Extensions_ServiceCollection.Configure_T_BinderOptions,
paramList: configParam + $", {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions}");
- _writer.WriteLine($"return {Identifier.Configure}<{Identifier.TOptions}>({Identifier.services}, {defaultNameExpr}, {Identifier.configuration}, {Identifier.configureOptions});");
+ _writer.WriteLine($"return {Identifier.Configure}<{Identifier.TOptions}>({Identifier.services}, {defaultNameExpr}, {Identifier.config}, {Identifier.configureOptions});");
EmitEndBlock();
}
// Core Configure method that the other overloads call.
// Like the others, it is public API that could be called directly by users.
// So, it is always generated whenever a Configure overload is called.
- string optionsNamespaceName = "Microsoft.Extensions.Options";
-
EmitStartMethod(MethodsToGen_Extensions_ServiceCollection.Configure_T_name_BinderOptions, paramList: $"string? {Identifier.name}, " + configParam + $", {TypeDisplayString.NullableActionOfBinderOptions} {Identifier.configureOptions}");
EmitCheckForNullArgument_WithBlankLine(Identifier.services);
- EmitCheckForNullArgument_WithBlankLine(Identifier.configuration);
+ EmitCheckForNullArgument_WithBlankLine(Identifier.config);
_writer.WriteLine($$"""
OptionsServiceCollectionExtensions.AddOptions({{Identifier.services}});
- {{Identifier.services}}.{{Identifier.AddSingleton}}<{{Identifier.IOptionsChangeTokenSource}}<{{Identifier.TOptions}}>>(new {{Identifier.ConfigurationChangeTokenSource}}<{{Identifier.TOptions}}>({{Identifier.name}}, {{Identifier.configuration}}));
- return {{Identifier.services}}.{{Identifier.AddSingleton}}<{{optionsNamespaceName}}.IConfigureOptions<{{Identifier.TOptions}}>>(new {{optionsNamespaceName}}.ConfigureNamedOptions<{{Identifier.TOptions}}>({{Identifier.name}}, {{Identifier.obj}} => {{nameof(MethodsToGen_CoreBindingHelper.BindCoreMain)}}({{Identifier.configuration}}, {{Identifier.obj}}, typeof({{Identifier.TOptions}}), {{Identifier.configureOptions}})));
+ {{Identifier.services}}.{{Identifier.AddSingleton}}<{{Identifier.IOptionsChangeTokenSource}}<{{Identifier.TOptions}}>>(new {{Identifier.ConfigurationChangeTokenSource}}<{{Identifier.TOptions}}>({{Identifier.name}}, {{Identifier.config}}));
+ return {{Identifier.services}}.{{Identifier.AddSingleton}}>(new ConfigureNamedOptions<{{Identifier.TOptions}}>({{Identifier.name}}, {{Identifier.instance}} => {{nameof(MethodsToGen_CoreBindingHelper.BindCoreMain)}}({{Identifier.config}}, {{Identifier.instance}}, typeof({{Identifier.TOptions}}), {{Identifier.configureOptions}})));
""");
EmitEndBlock();
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/MethodsToGen.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/MethodsToGen.cs
index 2c582b20e8ebd..6165a3e6d46dc 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/MethodsToGen.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/MethodsToGen.cs
@@ -27,17 +27,17 @@ internal enum MethodsToGen_ConfigurationBinder
None = 0x0,
///
- /// Bind(IConfiguration, object).
+ /// Bind(IConfiguration, object?).
///
Bind_instance = 0x1,
///
- /// Bind(IConfiguration, object, Action).
+ /// Bind(IConfiguration, object?, Action?).
///
Bind_instance_BinderOptions = 0x2,
///
- /// Bind(IConfiguration, string, object).
+ /// Bind(IConfiguration, string, object?).
///
Bind_key_instance = 0x4,
@@ -47,17 +47,17 @@ internal enum MethodsToGen_ConfigurationBinder
Get_T = 0x8,
///
- /// Get(IConfiguration, Action).
+ /// Get(IConfiguration, Action?).
///
Get_T_BinderOptions = 0x10,
///
- /// Get(IConfiguration, Type).
+ /// Get(IConfiguration, Type).
///
Get_TypeOf = 0x20,
///
- /// Get(IConfiguration, Type, Action).
+ /// Get(IConfiguration, Type, Action?).
///
Get_TypeOf_BinderOptions = 0x40,
@@ -67,7 +67,7 @@ internal enum MethodsToGen_ConfigurationBinder
GetValue_TypeOf_key = 0x80,
///
- /// GetValue(IConfiguration, Type, object).
+ /// GetValue(IConfiguration, Type, object?).
///
GetValue_TypeOf_key_defaultValue = 0x100,
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/ConfigurationBinder.cs
index e24ce11fe4374..bce3022249384 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/ConfigurationBinder.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/ConfigurationBinder.cs
@@ -72,7 +72,7 @@ private void RegisterBindInvocation(BinderInvocation invocation)
return;
}
- int objectIndex = overload switch
+ int instanceIndex = overload switch
{
MethodsToGen_ConfigurationBinder.Bind_instance => 1,
MethodsToGen_ConfigurationBinder.Bind_instance_BinderOptions => 1,
@@ -80,13 +80,13 @@ private void RegisterBindInvocation(BinderInvocation invocation)
_ => throw new InvalidOperationException()
};
- IArgumentOperation objectArg = operation.Arguments[objectIndex];
- if (objectArg.Parameter.Type.SpecialType != SpecialType.System_Object)
+ IArgumentOperation instanceArg = operation.Arguments[instanceIndex];
+ if (instanceArg.Parameter.Type.SpecialType != SpecialType.System_Object)
{
return;
}
- ITypeSymbol? type = ResolveType(objectArg.Value)?.WithNullableAnnotation(NullableAnnotation.None);
+ ITypeSymbol? type = ResolveType(instanceArg.Value)?.WithNullableAnnotation(NullableAnnotation.None);
if (!IsValidRootConfigType(type))
{
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsBuilderConfigurationExtensions.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsBuilderConfigurationExtensions.cs
index a62e63c0d90d5..f65c67a7878b1 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsBuilderConfigurationExtensions.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsBuilderConfigurationExtensions.cs
@@ -25,9 +25,11 @@ private void RegisterMethodInvocation_OptionsBuilderExt(BinderInvocation invocat
return;
}
- TypeSpec typeSpec = GetTargetTypeForRootInvocation(
- type: targetMethod.TypeArguments[0].WithNullableAnnotation(NullableAnnotation.None),
- invocation.Location);
+
+ ITypeSymbol? typeSymbol = targetMethod.TypeArguments[0].WithNullableAnnotation(NullableAnnotation.None);
+ // This would violate generic type constraint; any such invocation could not have been included in the initial parser.
+ Debug.Assert(typeSymbol?.IsValueType is not true);
+ TypeSpec typeSpec = GetTargetTypeForRootInvocation(typeSymbol, invocation.Location);
if (typeSpec is null)
{
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsConfigurationServiceCollectionExtensions.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsConfigurationServiceCollectionExtensions.cs
index c356b29a69eff..b811a8e6ae0ec 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsConfigurationServiceCollectionExtensions.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Parser/OptionsConfigurationServiceCollectionExtensions.cs
@@ -69,9 +69,10 @@ @params[1].Type.SpecialType is SpecialType.System_String &&
return;
}
- TypeSpec typeSpec = GetTargetTypeForRootInvocation(
- type: targetMethod.TypeArguments[0].WithNullableAnnotation(NullableAnnotation.None),
- invocation.Location);
+ ITypeSymbol? typeSymbol = targetMethod.TypeArguments[0].WithNullableAnnotation(NullableAnnotation.None);
+ // This would violate generic type constraint; any such invocation could not have been included in the initial parser.
+ Debug.Assert(typeSymbol?.IsValueType is not true);
+ TypeSpec typeSpec = GetTargetTypeForRootInvocation(typeSymbol, invocation.Location);
if (typeSpec is null)
{
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Model/ObjectSpec.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Model/ObjectSpec.cs
index 8cc0ba68b4938..09008dde159ad 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Model/ObjectSpec.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Model/ObjectSpec.cs
@@ -22,8 +22,6 @@ public ObjectSpec(INamedTypeSymbol type) : base(type) { }
public List ConstructorParameters { get; } = new();
- public override bool NeedsMemberBinding => CanInitialize &&
- Properties.Values.Count > 0 &&
- Properties.Values.Any(p => p.ShouldBind());
+ public override bool NeedsMemberBinding => Properties.Values.Any(p => p.ShouldBind());
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
index 8651e4922e0d7..74365d8084aa3 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
@@ -91,6 +91,7 @@ public static class ConfigurationBinder
Action? configureOptions)
{
ThrowHelper.ThrowIfNull(configuration);
+ ThrowHelper.ThrowIfNull(type);
var options = new BinderOptions();
configureOptions?.Invoke(options);
@@ -108,7 +109,10 @@ public static class ConfigurationBinder
[RequiresDynamicCode(DynamicCodeWarningMessage)]
[RequiresUnreferencedCode(InstanceGetTypeTrimmingWarningMessage)]
public static void Bind(this IConfiguration configuration, string key, object? instance)
- => configuration.GetSection(key).Bind(instance);
+ {
+ ThrowHelper.ThrowIfNull(configuration);
+ configuration.GetSection(key).Bind(instance);
+ }
///
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
@@ -200,6 +204,9 @@ public static void Bind(this IConfiguration configuration, object? instance, Act
Type type, string key,
object? defaultValue)
{
+ ThrowHelper.ThrowIfNull(configuration);
+ ThrowHelper.ThrowIfNull(type);
+
IConfigurationSection section = configuration.GetSection(key);
string? value = section.Value;
if (value != null)
@@ -305,125 +312,122 @@ private static void BindInstance(
return;
}
- if (config != null)
+ if (config.GetChildren().Any())
{
- if (config.GetChildren().Any())
+ // for arrays and read-only list-like interfaces, we concatenate on to what is already there, if we can
+ if (type.IsArray || IsImmutableArrayCompatibleInterface(type))
{
- // for arrays and read-only list-like interfaces, we concatenate on to what is already there, if we can
- if (type.IsArray || IsImmutableArrayCompatibleInterface(type))
+ if (!bindingPoint.IsReadOnly)
{
- if (!bindingPoint.IsReadOnly)
- {
- bindingPoint.SetValue(BindArray(type, (IEnumerable?)bindingPoint.Value, config, options));
- }
-
- // for getter-only collection properties that we can't add to, nothing more we can do
- return;
+ bindingPoint.SetValue(BindArray(type, (IEnumerable?)bindingPoint.Value, config, options));
}
- // -----------------------------------------------------------------------------------------------------------------------------
- // | bindingPoint | bindingPoint |
- // Interface | Value | IsReadOnly | Behavior
- // -----------------------------------------------------------------------------------------------------------------------------
- // ISet | not null | true/false | Use the Value instance to populate the configuration
- // ISet | null | false | Create HashSet instance to populate the configuration
- // ISet | null | true | nothing
- // IReadOnlySet | null/not null | false | Create HashSet instance, copy over existing values, and populate the configuration
- // IReadOnlySet | null/not null | true | nothing
- // -----------------------------------------------------------------------------------------------------------------------------
- if (TypeIsASetInterface(type))
- {
- if (!bindingPoint.IsReadOnly || bindingPoint.Value is not null)
- {
- object? newValue = BindSet(type, (IEnumerable?)bindingPoint.Value, config, options);
- if (!bindingPoint.IsReadOnly && newValue != null)
- {
- bindingPoint.SetValue(newValue);
- }
- }
-
- return;
- }
+ // for getter-only collection properties that we can't add to, nothing more we can do
+ return;
+ }
- // -----------------------------------------------------------------------------------------------------------------------------
- // | bindingPoint | bindingPoint |
- // Interface | Value | IsReadOnly | Behavior
- // -----------------------------------------------------------------------------------------------------------------------------
- // IDictionary | not null | true/false | Use the Value instance to populate the configuration
- // IDictionary | null | false | Create Dictionary instance to populate the configuration
- // IDictionary | null | true | nothing
- // IReadOnlyDictionary | null/not null | false | Create Dictionary instance, copy over existing values, and populate the configuration
- // IReadOnlyDictionary | null/not null | true | nothing
- // -----------------------------------------------------------------------------------------------------------------------------
- if (TypeIsADictionaryInterface(type))
+ // -----------------------------------------------------------------------------------------------------------------------------
+ // | bindingPoint | bindingPoint |
+ // Interface | Value | IsReadOnly | Behavior
+ // -----------------------------------------------------------------------------------------------------------------------------
+ // ISet | not null | true/false | Use the Value instance to populate the configuration
+ // ISet | null | false | Create HashSet instance to populate the configuration
+ // ISet | null | true | nothing
+ // IReadOnlySet | null/not null | false | Create HashSet instance, copy over existing values, and populate the configuration
+ // IReadOnlySet | null/not null | true | nothing
+ // -----------------------------------------------------------------------------------------------------------------------------
+ if (TypeIsASetInterface(type))
+ {
+ if (!bindingPoint.IsReadOnly || bindingPoint.Value is not null)
{
- if (!bindingPoint.IsReadOnly || bindingPoint.Value is not null)
+ object? newValue = BindSet(type, (IEnumerable?)bindingPoint.Value, config, options);
+ if (!bindingPoint.IsReadOnly && newValue != null)
{
- object? newValue = BindDictionaryInterface(bindingPoint.Value, type, config, options);
- if (!bindingPoint.IsReadOnly && newValue != null)
- {
- bindingPoint.SetValue(newValue);
- }
+ bindingPoint.SetValue(newValue);
}
-
- return;
}
- // If we don't have an instance, try to create one
- if (bindingPoint.Value is null)
+ return;
+ }
+
+ // -----------------------------------------------------------------------------------------------------------------------------
+ // | bindingPoint | bindingPoint |
+ // Interface | Value | IsReadOnly | Behavior
+ // -----------------------------------------------------------------------------------------------------------------------------
+ // IDictionary | not null | true/false | Use the Value instance to populate the configuration
+ // IDictionary | null | false | Create Dictionary instance to populate the configuration
+ // IDictionary | null | true | nothing
+ // IReadOnlyDictionary | null/not null | false | Create Dictionary instance, copy over existing values, and populate the configuration
+ // IReadOnlyDictionary | null/not null | true | nothing
+ // -----------------------------------------------------------------------------------------------------------------------------
+ if (TypeIsADictionaryInterface(type))
+ {
+ if (!bindingPoint.IsReadOnly || bindingPoint.Value is not null)
{
- // if the binding point doesn't let us set a new instance, there's nothing more we can do
- if (bindingPoint.IsReadOnly)
+ object? newValue = BindDictionaryInterface(bindingPoint.Value, type, config, options);
+ if (!bindingPoint.IsReadOnly && newValue != null)
{
- return;
+ bindingPoint.SetValue(newValue);
}
+ }
- Type? interfaceGenericType = type.IsInterface && type.IsConstructedGenericType ? type.GetGenericTypeDefinition() : null;
+ return;
+ }
- if (interfaceGenericType is not null &&
- (interfaceGenericType == typeof(ICollection<>) || interfaceGenericType == typeof(IList<>)))
- {
- // For ICollection and IList we bind them to mutable List type.
- Type genericType = typeof(List<>).MakeGenericType(type.GenericTypeArguments);
- bindingPoint.SetValue(Activator.CreateInstance(genericType));
- }
- else
- {
- bindingPoint.SetValue(CreateInstance(type, config, options));
- }
+ // If we don't have an instance, try to create one
+ if (bindingPoint.Value is null)
+ {
+ // if the binding point doesn't let us set a new instance, there's nothing more we can do
+ if (bindingPoint.IsReadOnly)
+ {
+ return;
}
- Debug.Assert(bindingPoint.Value is not null);
-
- // At this point we know that we have a non-null bindingPoint.Value, we just have to populate the items
- // using the IDictionary<> or ICollection<> interfaces, or properties using reflection.
- Type? dictionaryInterface = FindOpenGenericInterface(typeof(IDictionary<,>), type);
+ Type? interfaceGenericType = type.IsInterface && type.IsConstructedGenericType ? type.GetGenericTypeDefinition() : null;
- if (dictionaryInterface != null)
+ if (interfaceGenericType is not null &&
+ (interfaceGenericType == typeof(ICollection<>) || interfaceGenericType == typeof(IList<>)))
{
- BindDictionary(bindingPoint.Value, dictionaryInterface, config, options);
+ // For ICollection and IList we bind them to mutable List type.
+ Type genericType = typeof(List<>).MakeGenericType(type.GenericTypeArguments);
+ bindingPoint.SetValue(Activator.CreateInstance(genericType));
}
else
{
- Type? collectionInterface = FindOpenGenericInterface(typeof(ICollection<>), type);
- if (collectionInterface != null)
- {
- BindCollection(bindingPoint.Value, collectionInterface, config, options);
- }
- else
- {
- BindProperties(bindingPoint.Value, config, options);
- }
+ bindingPoint.SetValue(CreateInstance(type, config, options));
}
}
+
+ Debug.Assert(bindingPoint.Value is not null);
+
+ // At this point we know that we have a non-null bindingPoint.Value, we just have to populate the items
+ // using the IDictionary<> or ICollection<> interfaces, or properties using reflection.
+ Type? dictionaryInterface = FindOpenGenericInterface(typeof(IDictionary<,>), type);
+
+ if (dictionaryInterface != null)
+ {
+ BindDictionary(bindingPoint.Value, dictionaryInterface, config, options);
+ }
else
{
- if (isParentCollection)
+ Type? collectionInterface = FindOpenGenericInterface(typeof(ICollection<>), type);
+ if (collectionInterface != null)
+ {
+ BindCollection(bindingPoint.Value, collectionInterface, config, options);
+ }
+ else
{
- bindingPoint.TrySetValue(CreateInstance(type, config, options));
+ BindProperties(bindingPoint.Value, config, options);
}
}
}
+ else
+ {
+ if (isParentCollection)
+ {
+ bindingPoint.TrySetValue(CreateInstance(type, config, options));
+ }
+ }
}
[RequiresDynamicCode(DynamicCodeWarningMessage)]
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
index eea01092667e8..a7e0474028172 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
@@ -2098,14 +2098,9 @@ public void CanBindToObjectMembers()
TestBind(options => config.GetSection("Local").Bind(RemoteAuthenticationOptions.s_NonGenericField), obj => RemoteAuthenticationOptions.s_NonGenericField);
// No null refs.
-#if BUILDING_SOURCE_GENERATOR_TESTS
-
- Assert.Throws(() => config.GetSection("Local").Bind(new RemoteAuthenticationOptions().NullGenericProp));
- Assert.Throws(() => config.GetSection("Local").Bind(RemoteAuthenticationOptions.s_NullNonGenericField));
-#else
config.GetSection("Local").Bind(new RemoteAuthenticationOptions().NullGenericProp);
config.GetSection("Local").Bind(RemoteAuthenticationOptions.s_NullNonGenericField);
-#endif
+
static void TestBind(Action> configure, Func, OidcProviderOptions> getBindedProp)
{
var obj = new RemoteAuthenticationOptions();
@@ -2121,5 +2116,81 @@ public void BinderSupportsObjCreationInput()
// No diagnostic warning SYSLIB1104.
configuration.Bind(new GraphWithUnsupportedMember());
}
+
+ [Fact]
+ public void TestNullHandling_Get()
+ {
+ // Null configuration.
+ IConfiguration? configuration = null;
+
+ Assert.Throws(() => configuration.Get());
+ Assert.Throws(() => configuration.Get(_ => { }));
+ Assert.Throws(() => configuration.Get());
+ Assert.Throws(() => configuration.Get(_ => { }));
+
+ // Null Type.
+ configuration = TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}");
+#pragma warning disable SYSLIB1104 // The target type for a binder call could not be determined
+ Assert.Throws(() => configuration.Get(type: null));
+ Assert.Throws(() => configuration.Get(type: null, _ => { }));
+#pragma warning restore SYSLIB1104 // The target type for a binder call could not be determined
+ }
+
+ [Fact]
+ public void TestNullHandling_GetValue()
+ {
+ string key = "Longitude";
+
+ // Null configuration.
+ Test(configuration: null, key);
+
+ // Null type.
+ IConfiguration configuration = TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}");
+#pragma warning disable SYSLIB1104 // The target type for a binder call could not be determined
+ Assert.Throws(() => configuration.GetValue(type: null, key));
+ Assert.Throws(() => configuration.GetValue(type: null, key, defaultValue: null));
+#pragma warning restore SYSLIB1104 // The target type for a binder call could not be determined
+
+ // Null key.
+ Test(configuration: configuration, key: null);
+
+ void Test(IConfiguration? configuration, string? key)
+ {
+ Assert.Throws(() => configuration.GetValue(key));
+ Assert.Throws(() => configuration.GetValue(key, defaultValue: null));
+ Assert.Throws(() => configuration.GetValue(key));
+ Assert.Throws(() => configuration.GetValue(key, defaultValue: default));
+ TestUntypedOverloads(configuration: null, key);
+ }
+
+ void TestUntypedOverloads(IConfiguration? configuration, string? key)
+ {
+ Assert.Throws(() => configuration.GetValue(typeof(GeolocationClass), key));
+ Assert.Throws(() => configuration.GetValue(typeof(GeolocationClass), key, defaultValue: null));
+ Assert.Throws(() => configuration.GetValue(typeof(GeolocationClass), key, new GeolocationClass()));
+ Assert.Throws(() => configuration.GetValue(typeof(Geolocation), key));
+ Assert.Throws(() => configuration.GetValue(typeof(Geolocation), key, defaultValue: null));
+ Assert.Throws(() => configuration.GetValue(typeof(Geolocation), key, default(Geolocation)));
+ }
+ }
+
+ [Fact]
+ public void TestNullHandling_Bind()
+ {
+ // Null configuration.
+ IConfiguration? configuration = null;
+ GeolocationClass? location = new();
+ Assert.Throws(() => configuration.Bind(location));
+ Assert.Throws(() => configuration.Bind(location, _ => { }));
+ Assert.Throws(() => configuration.Bind("", location));
+
+ // Null object.
+ configuration = TestHelpers.GetConfigurationFromJsonString(@"{""Longitude"":1,""Latitude"":2}");
+ location = null;
+ // Expect no exceptions.
+ configuration.Bind(location);
+ configuration.Bind(location, _ => { });
+ configuration.Bind("", location);
+ }
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt
index 528847cfeb3a3..5f186e42e4da7 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/Collections.generated.txt
@@ -55,61 +55,61 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
if (type == typeof(Program.MyClassWithCustomCollections))
{
- var obj = new Program.MyClassWithCustomCollections();
- BindCore(configuration, ref obj, binderOptions);
- return obj;
+ var instance = new Program.MyClassWithCustomCollections();
+ BindCore(configuration, ref instance, binderOptions);
+ return instance;
}
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
- public static void BindCore(IConfiguration configuration, ref Program.CustomDictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.CustomDictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = ParseInt(value, () => section.Path);
+ instance[section.Key] = ParseInt(value, () => section.Path);
}
}
}
- public static void BindCore(IConfiguration configuration, ref Program.CustomList obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.CustomList instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(value);
+ instance.Add(value);
}
}
}
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref ICollection obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref ICollection instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref IReadOnlyList obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref IReadOnlyList instance, BinderOptions? binderOptions)
{
- if (obj is not ICollection temp)
+ if (instance is not ICollection temp)
{
return;
}
@@ -123,31 +123,31 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = ParseInt(value, () => section.Path);
+ instance[section.Key] = ParseInt(value, () => section.Path);
}
}
}
- public static void BindCore(IConfiguration configuration, ref IDictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref IDictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = ParseInt(value, () => section.Path);
+ instance[section.Key] = ParseInt(value, () => section.Path);
}
}
}
- public static void BindCore(IConfiguration configuration, ref IReadOnlyDictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref IReadOnlyDictionary instance, BinderOptions? binderOptions)
{
- if (obj is not IDictionary temp)
+ if (instance is not IDictionary temp)
{
return;
}
@@ -161,40 +161,40 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClassWithCustomCollections obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClassWithCustomCollections instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClassWithCustomCollections), s_configKeys_ProgramMyClassWithCustomCollections, configuration, binderOptions);
if (AsConfigWithChildren(configuration.GetSection("CustomDictionary")) is IConfigurationSection section1)
{
- Program.CustomDictionary? temp3 = obj.CustomDictionary;
+ Program.CustomDictionary? temp3 = instance.CustomDictionary;
temp3 ??= new Program.CustomDictionary();
BindCore(section1, ref temp3, binderOptions);
- obj.CustomDictionary = temp3;
+ instance.CustomDictionary = temp3;
}
if (AsConfigWithChildren(configuration.GetSection("CustomList")) is IConfigurationSection section4)
{
- Program.CustomList? temp6 = obj.CustomList;
+ Program.CustomList? temp6 = instance.CustomList;
temp6 ??= new Program.CustomList();
BindCore(section4, ref temp6, binderOptions);
- obj.CustomList = temp6;
+ instance.CustomList = temp6;
}
if (AsConfigWithChildren(configuration.GetSection("IReadOnlyList")) is IConfigurationSection section7)
{
- IReadOnlyList? temp9 = obj.IReadOnlyList;
+ IReadOnlyList? temp9 = instance.IReadOnlyList;
temp9 = temp9 is null ? new List() : new List(temp9);
BindCore(section7, ref temp9, binderOptions);
- obj.IReadOnlyList = temp9;
+ instance.IReadOnlyList = temp9;
}
if (AsConfigWithChildren(configuration.GetSection("IReadOnlyDictionary")) is IConfigurationSection section10)
{
- IReadOnlyDictionary? temp12 = obj.IReadOnlyDictionary;
+ IReadOnlyDictionary? temp12 = instance.IReadOnlyDictionary;
temp12 = temp12 is null ? new Dictionary() : temp12.ToDictionary(pair => pair.Key, pair => pair.Value);
BindCore(section10, ref temp12, binderOptions);
- obj.IReadOnlyDictionary = temp12;
+ instance.IReadOnlyDictionary = temp12;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt
index 36ac12fd31f83..8d1f02b4e62e1 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind.generated.txt
@@ -32,55 +32,55 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region IConfiguration extensions.
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
[InterceptsLocationAttribute(@"src-0.cs", 13, 18)]
- public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj)
+ public static void Bind_ProgramMyClass(this IConfiguration configuration, object? instance)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
- if (obj is null)
+ if (instance is null)
{
- throw new ArgumentNullException(nameof(obj));
+ return;
}
- var typedObj = (Program.MyClass)obj;
+ var typedObj = (Program.MyClass)instance;
BindCore(configuration, ref typedObj, binderOptions: null);
}
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
[InterceptsLocationAttribute(@"src-0.cs", 14, 24)]
- public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj, Action? configureOptions)
+ public static void Bind_ProgramMyClass(this IConfiguration configuration, object? instance, Action? configureOptions)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
- if (obj is null)
+ if (instance is null)
{
- throw new ArgumentNullException(nameof(obj));
+ return;
}
- var typedObj = (Program.MyClass)obj;
+ var typedObj = (Program.MyClass)instance;
BindCore(configuration, ref typedObj, GetBinderOptions(configureOptions));
}
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
[InterceptsLocationAttribute(@"src-0.cs", 15, 24)]
- public static void Bind_ProgramMyClass(this IConfiguration configuration, string key, object? obj)
+ public static void Bind_ProgramMyClass(this IConfiguration configuration, string key, object? instance)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
- if (obj is null)
+ if (instance is null)
{
- throw new ArgumentNullException(nameof(obj));
+ return;
}
- var typedObj = (Program.MyClass)obj;
+ var typedObj = (Program.MyClass)instance;
BindCore(configuration.GetSection(key), ref typedObj, binderOptions: null);
}
#endregion IConfiguration extensions.
@@ -88,73 +88,73 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region Core binding extensions.
private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" });
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = value;
+ instance[section.Key] = value;
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
- if (!(obj.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
+ if (!(instance.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
{
element = new Program.MyClass2();
}
- obj[section.Key] = element;
+ instance[section.Key] = element;
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value1)
{
- obj.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
- List? temp4 = obj.MyList;
+ List? temp4 = instance.MyList;
temp4 ??= new List();
BindCore(section2, ref temp4, binderOptions);
- obj.MyList = temp4;
+ instance.MyList = temp4;
}
if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
- Dictionary? temp7 = obj.MyDictionary;
+ Dictionary? temp7 = instance.MyDictionary;
temp7 ??= new Dictionary();
BindCore(section5, ref temp7, binderOptions);
- obj.MyDictionary = temp7;
+ instance.MyDictionary = temp7;
}
if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
- Dictionary? temp10 = obj.MyComplexDictionary;
+ Dictionary? temp10 = instance.MyComplexDictionary;
temp10 ??= new Dictionary();
BindCore(section8, ref temp10, binderOptions);
- obj.MyComplexDictionary = temp10;
+ instance.MyComplexDictionary = temp10;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt
index 02fb06957bb3d..f3008878ca3c5 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance.generated.txt
@@ -32,19 +32,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region IConfiguration extensions.
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
[InterceptsLocationAttribute(@"src-0.cs", 12, 20)]
- public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj)
+ public static void Bind_ProgramMyClass(this IConfiguration configuration, object? instance)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
- if (obj is null)
+ if (instance is null)
{
- throw new ArgumentNullException(nameof(obj));
+ return;
}
- var typedObj = (Program.MyClass)obj;
+ var typedObj = (Program.MyClass)instance;
BindCore(configuration, ref typedObj, binderOptions: null);
}
#endregion IConfiguration extensions.
@@ -52,73 +52,73 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region Core binding extensions.
private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" });
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = value;
+ instance[section.Key] = value;
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
- if (!(obj.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
+ if (!(instance.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
{
element = new Program.MyClass2();
}
- obj[section.Key] = element;
+ instance[section.Key] = element;
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value1)
{
- obj.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
- List? temp4 = obj.MyList;
+ List? temp4 = instance.MyList;
temp4 ??= new List();
BindCore(section2, ref temp4, binderOptions);
- obj.MyList = temp4;
+ instance.MyList = temp4;
}
if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
- Dictionary? temp7 = obj.MyDictionary;
+ Dictionary? temp7 = instance.MyDictionary;
temp7 ??= new Dictionary();
BindCore(section5, ref temp7, binderOptions);
- obj.MyDictionary = temp7;
+ instance.MyDictionary = temp7;
}
if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
- Dictionary? temp10 = obj.MyComplexDictionary;
+ Dictionary? temp10 = instance.MyComplexDictionary;
temp10 ??= new Dictionary();
BindCore(section8, ref temp10, binderOptions);
- obj.MyComplexDictionary = temp10;
+ instance.MyComplexDictionary = temp10;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt
index 4703980996b88..19b955b6ea8bf 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Instance_BinderOptions.generated.txt
@@ -32,19 +32,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region IConfiguration extensions.
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
[InterceptsLocationAttribute(@"src-0.cs", 12, 20)]
- public static void Bind_ProgramMyClass(this IConfiguration configuration, object? obj, Action? configureOptions)
+ public static void Bind_ProgramMyClass(this IConfiguration configuration, object? instance, Action? configureOptions)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
- if (obj is null)
+ if (instance is null)
{
- throw new ArgumentNullException(nameof(obj));
+ return;
}
- var typedObj = (Program.MyClass)obj;
+ var typedObj = (Program.MyClass)instance;
BindCore(configuration, ref typedObj, GetBinderOptions(configureOptions));
}
#endregion IConfiguration extensions.
@@ -52,73 +52,73 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region Core binding extensions.
private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" });
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = value;
+ instance[section.Key] = value;
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
- if (!(obj.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
+ if (!(instance.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
{
element = new Program.MyClass2();
}
- obj[section.Key] = element;
+ instance[section.Key] = element;
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value1)
{
- obj.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
- List? temp4 = obj.MyList;
+ List? temp4 = instance.MyList;
temp4 ??= new List();
BindCore(section2, ref temp4, binderOptions);
- obj.MyList = temp4;
+ instance.MyList = temp4;
}
if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
- Dictionary? temp7 = obj.MyDictionary;
+ Dictionary? temp7 = instance.MyDictionary;
temp7 ??= new Dictionary();
BindCore(section5, ref temp7, binderOptions);
- obj.MyDictionary = temp7;
+ instance.MyDictionary = temp7;
}
if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
- Dictionary? temp10 = obj.MyComplexDictionary;
+ Dictionary? temp10 = instance.MyComplexDictionary;
temp10 ??= new Dictionary();
BindCore(section8, ref temp10, binderOptions);
- obj.MyComplexDictionary = temp10;
+ instance.MyComplexDictionary = temp10;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt
index 9937129699716..8533828221175 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Bind_Key_Instance.generated.txt
@@ -32,19 +32,19 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region IConfiguration extensions.
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
[InterceptsLocationAttribute(@"src-0.cs", 12, 20)]
- public static void Bind_ProgramMyClass(this IConfiguration configuration, string key, object? obj)
+ public static void Bind_ProgramMyClass(this IConfiguration configuration, string key, object? instance)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
- if (obj is null)
+ if (instance is null)
{
- throw new ArgumentNullException(nameof(obj));
+ return;
}
- var typedObj = (Program.MyClass)obj;
+ var typedObj = (Program.MyClass)instance;
BindCore(configuration.GetSection(key), ref typedObj, binderOptions: null);
}
#endregion IConfiguration extensions.
@@ -52,73 +52,73 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region Core binding extensions.
private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyDictionary", "MyComplexDictionary" });
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = value;
+ instance[section.Key] = value;
}
}
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
- if (!(obj.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
+ if (!(instance.TryGetValue(section.Key, out Program.MyClass2? element) && element is not null))
{
element = new Program.MyClass2();
}
- obj[section.Key] = element;
+ instance[section.Key] = element;
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value1)
{
- obj.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
- List? temp4 = obj.MyList;
+ List? temp4 = instance.MyList;
temp4 ??= new List();
BindCore(section2, ref temp4, binderOptions);
- obj.MyList = temp4;
+ instance.MyList = temp4;
}
if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
- Dictionary? temp7 = obj.MyDictionary;
+ Dictionary? temp7 = instance.MyDictionary;
temp7 ??= new Dictionary();
BindCore(section5, ref temp7, binderOptions);
- obj.MyDictionary = temp7;
+ instance.MyDictionary = temp7;
}
if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
- Dictionary? temp10 = obj.MyComplexDictionary;
+ Dictionary? temp10 = instance.MyComplexDictionary;
temp10 ??= new Dictionary();
BindCore(section8, ref temp10, binderOptions);
- obj.MyComplexDictionary = temp10;
+ instance.MyComplexDictionary = temp10;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt
index 3e6ce1459b289..4acbebff935bc 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get.generated.txt
@@ -67,94 +67,94 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
if (type == typeof(Program.MyClass))
{
- var obj = new Program.MyClass();
- BindCore(configuration, ref obj, binderOptions);
- return obj;
+ var instance = new Program.MyClass();
+ BindCore(configuration, ref instance, binderOptions);
+ return instance;
}
else if (type == typeof(Program.MyClass2))
{
- var obj = new Program.MyClass2();
- BindCore(configuration, ref obj, binderOptions);
- return obj;
+ var instance = new Program.MyClass2();
+ BindCore(configuration, ref instance, binderOptions);
+ return instance;
}
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref int[] obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref int[] instance, BinderOptions? binderOptions)
{
var temp2 = new List();
BindCore(configuration, ref temp2, binderOptions);
- int originalCount = obj.Length;
- Array.Resize(ref obj, originalCount + temp2.Count);
- temp2.CopyTo(obj, originalCount);
+ int originalCount = instance.Length;
+ Array.Resize(ref instance, originalCount + temp2.Count);
+ temp2.CopyTo(instance, originalCount);
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = value;
+ instance[section.Key] = value;
}
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value5)
{
- obj.MyInt = ParseInt(value5, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value5, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section6)
{
- List? temp8 = obj.MyList;
+ List? temp8 = instance.MyList;
temp8 ??= new List();
BindCore(section6, ref temp8, binderOptions);
- obj.MyList = temp8;
+ instance.MyList = temp8;
}
if (AsConfigWithChildren(configuration.GetSection("MyArray")) is IConfigurationSection section9)
{
- int[]? temp11 = obj.MyArray;
+ int[]? temp11 = instance.MyArray;
temp11 ??= new int[0];
BindCore(section9, ref temp11, binderOptions);
- obj.MyArray = temp11;
+ instance.MyArray = temp11;
}
if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section12)
{
- Dictionary? temp14 = obj.MyDictionary;
+ Dictionary? temp14 = instance.MyDictionary;
temp14 ??= new Dictionary();
BindCore(section12, ref temp14, binderOptions);
- obj.MyDictionary = temp14;
+ instance.MyDictionary = temp14;
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass2 obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions);
if (configuration["MyInt"] is string value15)
{
- obj.MyInt = ParseInt(value15, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value15, () => configuration.GetSection("MyInt").Path);
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt
index 85d0901de3ff6..fb5cf5e4b5ca3 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T.generated.txt
@@ -54,78 +54,78 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
if (type == typeof(Program.MyClass))
{
- var obj = new Program.MyClass();
- BindCore(configuration, ref obj, binderOptions);
- return obj;
+ var instance = new Program.MyClass();
+ BindCore(configuration, ref instance, binderOptions);
+ return instance;
}
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref int[] obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref int[] instance, BinderOptions? binderOptions)
{
var temp1 = new List();
BindCore(configuration, ref temp1, binderOptions);
- int originalCount = obj.Length;
- Array.Resize(ref obj, originalCount + temp1.Count);
- temp1.CopyTo(obj, originalCount);
+ int originalCount = instance.Length;
+ Array.Resize(ref instance, originalCount + temp1.Count);
+ temp1.CopyTo(instance, originalCount);
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = value;
+ instance[section.Key] = value;
}
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value4)
{
- obj.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5)
{
- List? temp7 = obj.MyList;
+ List? temp7 = instance.MyList;
temp7 ??= new List();
BindCore(section5, ref temp7, binderOptions);
- obj.MyList = temp7;
+ instance.MyList = temp7;
}
if (AsConfigWithChildren(configuration.GetSection("MyArray")) is IConfigurationSection section8)
{
- int[]? temp10 = obj.MyArray;
+ int[]? temp10 = instance.MyArray;
temp10 ??= new int[0];
BindCore(section8, ref temp10, binderOptions);
- obj.MyArray = temp10;
+ instance.MyArray = temp10;
}
if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
{
- Dictionary? temp13 = obj.MyDictionary;
+ Dictionary? temp13 = instance.MyDictionary;
temp13 ??= new Dictionary();
BindCore(section11, ref temp13, binderOptions);
- obj.MyDictionary = temp13;
+ instance.MyDictionary = temp13;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt
index d394cc7b269f7..ac8e362b4e2e0 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_T_BinderOptions.generated.txt
@@ -54,78 +54,78 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
if (type == typeof(Program.MyClass))
{
- var obj = new Program.MyClass();
- BindCore(configuration, ref obj, binderOptions);
- return obj;
+ var instance = new Program.MyClass();
+ BindCore(configuration, ref instance, binderOptions);
+ return instance;
}
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref int[] obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref int[] instance, BinderOptions? binderOptions)
{
var temp1 = new List();
BindCore(configuration, ref temp1, binderOptions);
- int originalCount = obj.Length;
- Array.Resize(ref obj, originalCount + temp1.Count);
- temp1.CopyTo(obj, originalCount);
+ int originalCount = instance.Length;
+ Array.Resize(ref instance, originalCount + temp1.Count);
+ temp1.CopyTo(instance, originalCount);
}
- public static void BindCore(IConfiguration configuration, ref Dictionary obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Dictionary instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj[section.Key] = value;
+ instance[section.Key] = value;
}
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value4)
{
- obj.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5)
{
- List? temp7 = obj.MyList;
+ List? temp7 = instance.MyList;
temp7 ??= new List();
BindCore(section5, ref temp7, binderOptions);
- obj.MyList = temp7;
+ instance.MyList = temp7;
}
if (AsConfigWithChildren(configuration.GetSection("MyArray")) is IConfigurationSection section8)
{
- int[]? temp10 = obj.MyArray;
+ int[]? temp10 = instance.MyArray;
temp10 ??= new int[0];
BindCore(section8, ref temp10, binderOptions);
- obj.MyArray = temp10;
+ instance.MyArray = temp10;
}
if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
{
- Dictionary? temp13 = obj.MyDictionary;
+ Dictionary? temp13 = instance.MyDictionary;
temp13 ??= new Dictionary();
BindCore(section11, ref temp13, binderOptions);
- obj.MyDictionary = temp13;
+ instance.MyDictionary = temp13;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt
index 83cd88561310a..5d0088b908162 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf.generated.txt
@@ -54,21 +54,21 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
if (type == typeof(Program.MyClass2))
{
- var obj = new Program.MyClass2();
- BindCore(configuration, ref obj, binderOptions);
- return obj;
+ var instance = new Program.MyClass2();
+ BindCore(configuration, ref instance, binderOptions);
+ return instance;
}
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass2 obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions);
if (configuration["MyInt"] is string value1)
{
- obj.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt
index 91714c80cd013..9da3a3a22e9b7 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/ConfigurationBinder/Get_TypeOf_BinderOptions.generated.txt
@@ -54,21 +54,21 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
if (type == typeof(Program.MyClass2))
{
- var obj = new Program.MyClass2();
- BindCore(configuration, ref obj, binderOptions);
- return obj;
+ var instance = new Program.MyClass2();
+ BindCore(configuration, ref instance, binderOptions);
+ return instance;
}
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass2 obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass2 instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass2), s_configKeys_ProgramMyClass2, configuration, binderOptions);
if (configuration["MyInt"] is string value1)
{
- obj.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value1, () => configuration.GetSection("MyInt").Path);
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt
index 88b35037c22c3..96e389be89560 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/BindConfiguration.generated.txt
@@ -34,7 +34,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region OptionsBuilder extensions.
/// Registers the dependency injection container to bind against the obtained from the DI service provider.
[InterceptsLocationAttribute(@"src-0.cs", 12, 24)]
- public static OptionsBuilder BindConfiguration(this OptionsBuilder optionsBuilder, string configSectionPath, Action? configureOptions = null) where TOptions : class
+ public static OptionsBuilder BindConfiguration(this OptionsBuilder optionsBuilder, string configSectionPath, Action? configureBinder = null) where TOptions : class
{
if (optionsBuilder is null)
{
@@ -46,20 +46,15 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
throw new ArgumentNullException(nameof(configSectionPath));
}
- optionsBuilder.Configure((obj, configuration) =>
+ optionsBuilder.Configure((instance, config) =>
{
- if (obj is null)
+ if (config is null)
{
- throw new ArgumentNullException(nameof(obj));
+ throw new ArgumentNullException(nameof(config));
}
- if (configuration is null)
- {
- throw new ArgumentNullException(nameof(configuration));
- }
-
- IConfiguration section = string.Equals(string.Empty, configSectionPath, StringComparison.OrdinalIgnoreCase) ? configuration : configuration.GetSection(configSectionPath);
- BindCoreMain(section, obj, typeof(TOptions), configureOptions);
+ IConfiguration section = string.Equals(string.Empty, configSectionPath, StringComparison.OrdinalIgnoreCase) ? config : config.GetSection(configSectionPath);
+ BindCoreMain(section, instance, typeof(TOptions), configureBinder);
});
optionsBuilder.Services.AddSingleton, ConfigurationChangeTokenSource>();
@@ -70,16 +65,11 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region Core binding extensions.
private readonly static Lazy> s_configKeys_ProgramMyClass = new(() => new HashSet(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList" });
- public static void BindCoreMain(IConfiguration configuration, object obj, Type type, Action? configureOptions)
+ public static void BindCoreMain(IConfiguration configuration, object instance, Type type, Action? configureOptions)
{
- if (configuration is null)
+ if (instance is null)
{
- throw new ArgumentNullException(nameof(configuration));
- }
-
- if (obj is null)
- {
- throw new ArgumentNullException(nameof(obj));
+ return;
}
if (!HasValueOrChildren(configuration))
@@ -91,7 +81,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
if (type == typeof(Program.MyClass))
{
- var temp = (Program.MyClass)obj;
+ var temp = (Program.MyClass)instance;
BindCore(configuration, ref temp, binderOptions);
return;
}
@@ -99,34 +89,34 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
}
- public static void BindCore(IConfiguration configuration, ref List obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref List instance, BinderOptions? binderOptions)
{
foreach (IConfigurationSection section in configuration.GetChildren())
{
if (section.Value is string value)
{
- obj.Add(ParseInt(value, () => section.Path));
+ instance.Add(ParseInt(value, () => section.Path));
}
}
}
- public static void BindCore(IConfiguration configuration, ref Program.MyClass obj, BinderOptions? binderOptions)
+ public static void BindCore(IConfiguration configuration, ref Program.MyClass instance, BinderOptions? binderOptions)
{
ValidateConfigurationKeys(typeof(Program.MyClass), s_configKeys_ProgramMyClass, configuration, binderOptions);
- obj.MyString = configuration["MyString"]!;
+ instance.MyString = configuration["MyString"]!;
if (configuration["MyInt"] is string value2)
{
- obj.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path);
+ instance.MyInt = ParseInt(value2, () => configuration.GetSection("MyInt").Path);
}
if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section3)
{
- List? temp5 = obj.MyList;
+ List? temp5 = instance.MyList;
temp5 ??= new List();
BindCore(section3, ref temp5, binderOptions);
- obj.MyList = temp5;
+ instance.MyList = temp5;
}
}
diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt
index ff590d8398f3a..9755ab0df0008 100644
--- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt
+++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines/OptionsBuilder/Bind_T.generated.txt
@@ -34,57 +34,52 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
#region OptionsBuilder extensions.
/// Registers a configuration instance which will bind against.
[InterceptsLocationAttribute(@"src-0.cs", 15, 24)]
- public static OptionsBuilder Bind(this OptionsBuilder optionsBuilder, IConfiguration configuration) where TOptions : class
+ public static OptionsBuilder Bind(this OptionsBuilder optionsBuilder, IConfiguration config) where TOptions : class
{
- return Bind(optionsBuilder, configuration, configureOptions: null);
+ return Bind(optionsBuilder, config, configureBinder: null);
}
/// Registers a configuration instance which will bind against.
- public static OptionsBuilder Bind(this OptionsBuilder optionsBuilder, IConfiguration configuration, Action? configureOptions) where TOptions : class
+ public static OptionsBuilder Bind(this OptionsBuilder optionsBuilder, IConfiguration config, Action? configureBinder) where TOptions : class
{
if (optionsBuilder is null)
{
throw new ArgumentNullException(nameof(optionsBuilder));
}
- Configure(optionsBuilder.Services, optionsBuilder.Name, configuration, configureOptions);
+ Configure(optionsBuilder.Services, optionsBuilder.Name, config, configureBinder);
return optionsBuilder;
}
#endregion OptionsBuilder extensions.
#region IServiceCollection extensions.
/// Registers a configuration instance which TOptions will bind against.
- public static IServiceCollection Configure(this IServiceCollection services, string? name, IConfiguration configuration, Action? configureOptions) where TOptions : class
+ public static IServiceCollection Configure(this IServiceCollection services, string? name, IConfiguration config, Action