-
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 4 commits
8d7d920
1493935
ea81eba
a7761d5
36c25ca
438a77c
b8c73f9
4f7a43c
c047d04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -317,7 +317,7 @@ private static void BindInstance( | |
| // for sets and read-only set interfaces, we clone what's there into a new collection, if we can | ||
| 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 +530,40 @@ 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]; | ||
|
|
||
| if (orig != null) | ||
| MethodInfo? addMethod = dictionaryType.GetMethod("Add", DeclaredOnlyLookup); | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (addMethod is null || source is null) | ||
| { | ||
| foreach (object? item in orig) | ||
| dictionaryType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); | ||
| var dictionary = Activator.CreateInstance(dictionaryType); | ||
|
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. object? dictionary = Activator.CreateInstance(dictionaryType);
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, make sense. |
||
| addMethod = dictionaryType.GetMethod("Add", DeclaredOnlyLookup); | ||
|
|
||
| var orig = source as IEnumerable; | ||
| if (orig is not null) | ||
| { | ||
| object? k = keyMethod.GetMethod!.Invoke(item, null); | ||
| object? v = valueMethod.GetMethod!.Invoke(item, null); | ||
| arguments[0] = k; | ||
| arguments[1] = v; | ||
| addMethod.Invoke(dictionary, arguments); | ||
| Type kvpType = typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType); | ||
| PropertyInfo keyMethod = kvpType.GetProperty("Key", DeclaredOnlyLookup)!; | ||
| PropertyInfo valueMethod = kvpType.GetProperty("Value", DeclaredOnlyLookup)!; | ||
| object?[] arguments = new object?[2]; | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
|
||
| source = dictionary; | ||
| } | ||
|
|
||
| BindDictionary(dictionary, genericType, config, options); | ||
| Debug.Assert(source is not null); | ||
| Debug.Assert(addMethod is not null); | ||
|
|
||
| return dictionary; | ||
| BindDictionary(source, dictionaryType, config, options); | ||
|
|
||
| return source; | ||
| } | ||
|
|
||
| // Binds and potentially overwrites a dictionary object. | ||
|
|
@@ -733,36 +740,42 @@ 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 = keyType.IsEnum; | ||
| 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. |
||
|
|
||
| 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]; | ||
|
|
||
| if (source != null) | ||
| MethodInfo? addMethod = type.GetMethod("Add", DeclaredOnlyLookup); | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (addMethod is null || source is null) | ||
| { | ||
| foreach (object? item in source) | ||
| Type genericType = typeof(HashSet<>).MakeGenericType(elementType); | ||
| object instance = Activator.CreateInstance(genericType)!; | ||
| addMethod = genericType.GetMethod("Add", DeclaredOnlyLookup); | ||
|
|
||
| var orig = source as IEnumerable; | ||
| if (orig != null) | ||
| { | ||
| arguments[0] = item; | ||
| addMethod.Invoke(instance, arguments); | ||
| foreach (object? item in orig) | ||
| { | ||
| arguments[0] = item; | ||
| addMethod!.Invoke(instance, arguments); | ||
| } | ||
| } | ||
|
|
||
| source = instance; | ||
| } | ||
|
|
||
| Debug.Assert(source is not null); | ||
| Debug.Assert(addMethod is not null); | ||
|
|
||
| foreach (IConfigurationSection section in config.GetChildren()) | ||
| { | ||
| var itemBindingPoint = new BindingPoint(); | ||
|
|
@@ -777,7 +790,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 +803,7 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co | |
| } | ||
| } | ||
|
|
||
| return instance; | ||
| return source; | ||
| } | ||
|
|
||
| [RequiresUnreferencedCode(TrimmingWarningMessage)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment now incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to remove the word
setsfrom the comment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.