-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix Configuration Binding with Instantiated Objects #81050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8d7d920
1493935
ea81eba
a7761d5
36c25ca
438a77c
b8c73f9
4f7a43c
c047d04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -302,10 +302,12 @@ private static void BindInstance( | |
|
|
||
| if (config != null && 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 we concatenate on to what is already there, if we can | ||
| // for read-only list-like interfaces, we do the same only if the bindingPoint.Value is null. | ||
| bool isImmutableArrayCompatibleInterface = IsImmutableArrayCompatibleInterface(type); | ||
| if (type.IsArray || isImmutableArrayCompatibleInterface) | ||
| { | ||
| if (!bindingPoint.IsReadOnly) | ||
| if (!bindingPoint.IsReadOnly && (!isImmutableArrayCompatibleInterface || bindingPoint.Value is null)) | ||
| { | ||
| bindingPoint.SetValue(BindArray(type, (IEnumerable?)bindingPoint.Value, config, options)); | ||
| } | ||
|
|
@@ -317,7 +319,7 @@ private static void BindInstance( | |
| // for sets and read-only set interfaces, we clone what's there into a new collection, if we can | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment now incorrect?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to remove the word
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the comment needs to be beefed up a bit to describe the whole behavior. |
||
| if (TypeIsASetInterface(type) && !bindingPoint.IsReadOnly) | ||
| { | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| object? newValue = BindSet(type, (IEnumerable?)bindingPoint.Value, config, options); | ||
| object? newValue = BindSet(type, bindingPoint.Value, config, options); | ||
| if (newValue != null) | ||
| { | ||
| bindingPoint.SetValue(newValue); | ||
|
|
@@ -530,33 +532,26 @@ private static bool CanBindToTheseConstructorParameters(ParameterInfo[] construc | |
| return null; | ||
| } | ||
|
|
||
| Type genericType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); | ||
| MethodInfo addMethod = genericType.GetMethod("Add", DeclaredOnlyLookup)!; | ||
|
|
||
| Type kvpType = typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType); | ||
| PropertyInfo keyMethod = kvpType.GetProperty("Key", DeclaredOnlyLookup)!; | ||
| PropertyInfo valueMethod = kvpType.GetProperty("Value", DeclaredOnlyLookup)!; | ||
|
|
||
| object dictionary = Activator.CreateInstance(genericType)!; | ||
|
|
||
| var orig = source as IEnumerable; | ||
| object?[] arguments = new object?[2]; | ||
| MethodInfo? addMethod = dictionaryType.GetMethod("Add", DeclaredOnlyLookup); | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (addMethod is null && source is not null) | ||
| { | ||
| // Instantiated IReadOnlyDictionary cannot be mutated by the configuration. | ||
| return null; | ||
| } | ||
|
|
||
| if (orig != null) | ||
| if (source is null) | ||
| { | ||
| foreach (object? item in orig) | ||
| { | ||
| object? k = keyMethod.GetMethod!.Invoke(item, null); | ||
| object? v = valueMethod.GetMethod!.Invoke(item, null); | ||
| arguments[0] = k; | ||
| arguments[1] = v; | ||
| addMethod.Invoke(dictionary, arguments); | ||
| } | ||
| dictionaryType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); | ||
| source = Activator.CreateInstance(dictionaryType); | ||
| addMethod ??= dictionaryType.GetMethod("Add", DeclaredOnlyLookup)!; | ||
| } | ||
|
|
||
| BindDictionary(dictionary, genericType, config, options); | ||
| Debug.Assert(source is not null); | ||
| Debug.Assert(addMethod is not null); | ||
|
|
||
| BindDictionary(source, dictionaryType, config, options); | ||
|
|
||
| return dictionary; | ||
| return source; | ||
| } | ||
|
|
||
| // Binds and potentially overwrites a dictionary object. | ||
|
|
@@ -733,36 +728,37 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co | |
|
|
||
| [RequiresDynamicCode(DynamicCodeWarningMessage)] | ||
| [RequiresUnreferencedCode("Cannot statically analyze what the element type is of the Array so its members may be trimmed.")] | ||
| private static object? BindSet(Type type, IEnumerable? source, IConfiguration config, BinderOptions options) | ||
| private static object? BindSet(Type type, object? source, IConfiguration config, BinderOptions options) | ||
tarekgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| Type elementType = type.GetGenericArguments()[0]; | ||
|
|
||
| Type keyType = type.GenericTypeArguments[0]; | ||
| bool keyTypeIsEnum = elementType.IsEnum; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
bool elementTypeIsEnum = elementType.IsEnum;
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. |
||
|
|
||
| bool keyTypeIsEnum = keyType.IsEnum; | ||
|
|
||
| if (keyType != typeof(string) && !keyTypeIsEnum) | ||
| if (elementType != typeof(string) && !keyTypeIsEnum) | ||
| { | ||
| // We only support string and enum keys | ||
| return null; | ||
| } | ||
|
|
||
| Type genericType = typeof(HashSet<>).MakeGenericType(keyType); | ||
| object instance = Activator.CreateInstance(genericType)!; | ||
|
|
||
| MethodInfo addMethod = genericType.GetMethod("Add", DeclaredOnlyLookup)!; | ||
|
|
||
| object?[] arguments = new object?[1]; | ||
| MethodInfo? addMethod = type.GetMethod("Add", DeclaredOnlyLookup); | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (addMethod is null && source is not null) | ||
| { | ||
| // It is readonly Set interface. We bind the readonly interfaces only when its value is not instantiated. | ||
| return null; | ||
| } | ||
|
|
||
| if (source != null) | ||
| if (source is null) | ||
| { | ||
| foreach (object? item in source) | ||
| { | ||
| arguments[0] = item; | ||
| addMethod.Invoke(instance, arguments); | ||
| } | ||
| Type genericType = typeof(HashSet<>).MakeGenericType(elementType); | ||
| source = Activator.CreateInstance(genericType)!; | ||
| addMethod ??= genericType.GetMethod("Add", DeclaredOnlyLookup)!; | ||
| } | ||
|
|
||
| Debug.Assert(source is not null); | ||
| Debug.Assert(addMethod is not null); | ||
|
|
||
| object?[] arguments = new object?[1]; | ||
|
|
||
| foreach (IConfigurationSection section in config.GetChildren()) | ||
| { | ||
| var itemBindingPoint = new BindingPoint(); | ||
|
|
@@ -777,7 +773,7 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co | |
| { | ||
| arguments[0] = itemBindingPoint.Value; | ||
|
|
||
| addMethod.Invoke(instance, arguments); | ||
| addMethod.Invoke(source, arguments); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
|
|
@@ -790,7 +786,7 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co | |
| } | ||
| } | ||
|
|
||
| return instance; | ||
| return source; | ||
| } | ||
|
|
||
| [RequiresUnreferencedCode(TrimmingWarningMessage)] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.