ActivatorUtilities.CreateInstance() should respect [ActivatorUtilitiesConstructor]#99175
Merged
steveharter merged 4 commits intodotnet:mainfrom Mar 6, 2024
Merged
ActivatorUtilities.CreateInstance() should respect [ActivatorUtilitiesConstructor]#99175steveharter merged 4 commits intodotnet:mainfrom
steveharter merged 4 commits intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsAddress a case from #98959 where ordering of the constructors can cause a constructor that has
|
steveharter
commented
Mar 1, 2024
...libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ActivatorUtilitiesTests.cs
Show resolved
Hide resolved
tarekgh
reviewed
Mar 1, 2024
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
Outdated
Show resolved
Hide resolved
tarekgh
reviewed
Mar 1, 2024
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
Outdated
Show resolved
Hide resolved
tarekgh
reviewed
Mar 5, 2024
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
Outdated
Show resolved
Hide resolved
tarekgh
approved these changes
Mar 5, 2024
Member
tarekgh
left a comment
There was a problem hiding this comment.
LGTM, I hope the breaking change will not cause any issue. At least now we are behaving as our docs describe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current approach of selecting constructors uses
[ActivatorUtilitiesConstructor]in an odd manner that even when a constructor has the attribute, it may not be called depending on the ordering of constructors and the number of constructor parameters. This PR changes to always call the constructor that has that attribute.Current approach:
[ActivatorUtilitiesConstructor]then that constructor is selected, no matter how many parameters are present. Only one constructor can have the attribute.[ActivatorUtilitiesConstructor]was found, then that is selected.Proposed approach: from the current approach above, remove (4) which makes (1) no longer applicable.
This address a case from #98959 where ordering of the constructors can cause a constructor that has
[ActivatorUtilitiesConstructor]to not be selected if comes before a different constructor with the same parameter count, and also fully addresses the issues raised in #42339 (comment).cc @tarekgh, @eerhardt