Repository navigation
Fix resolving types with primary constructors using in parameters - #105928
pseudonym117 wants to merge 1 commit into
Conversation
|
@dotnet-policy-service agree |
|
Can you elaborate on why you want to inject a service with |
|
I would ask the inverse of that - is there any reason why Actual use-case here was that I wanted to both use a primary constructor for simplicity, and wanted injected services to be It is possible that this is not the most correct fix - anecdotally it does resolve the issue for my use-case though. Is there a more correct way to detect |
A value type A reference type A An
So consider the following So |
|
Thanks for the detailed explanation - I definitely learned a bit here. However, I believe you may be overlooking the behavior of Without this pattern does require adding |
The behavior doesn't change, it is still taking the value byref and still blocking the same scenarios. Not using
|
|
Yes, but injecting and assigning as Implementing In either case, I think that my question from above still stands:
|
|
We should not be checking into main for V9. See #105924 (comment). |
As indicated above, this doesn't do what you think it does and doesn't add any actual protection, its just adding unnecessary cost to the callsite. The right thing to do is simply:
For proper usages of |
Is there a different branch this can target to be included in V10?
The example you provided does not solve my concern, and in fact makes user-errors more likely. While this does create a I would argue that this pattern actually makes human-errors more likely, as there will be 2 variables, typically with near-identical names, in scope for IntelliSense to recommend.
So from what I understand, you agree that the DependencyInjection library should support injection of |
Not quite. The sentiment is that some valid scenario may exist, in which case it may be valid to add that support. But that support should be considered in the context of a valid and real world usage scenario. Such a scenario needs to be explicitly detailed with the relevant rational, tests, etc. Adding features/support should not be considered or driven with regards to non-typical usage scenarios; such as using
This is no different than existing constructor parameters or private fields/public properties which have existed for the 20+ year history of C#/.NET. Auto properties were not always a thing, even with auto properties you often need logic on top and so it is extremely typical to have one, both, or all of
The "correct" solution here would be to write an analyzer that flags the usage you don't want to have happen in your codebase. The next best thing would be to log a proposal for a new feature on https://github.com/dotnet/csharplang detailing that you'd like a way to specify that primary constructor variables are only available for field/property assignment and are not available in derived contexts, as you'd like to enforce that users go through the field/property instead. |
|
Closing; see #105924 (comment) |
Fixes #105924
inprimary constructor parameters are treated as reference types when inspected. Fix here is simply to use the referenced type instead when resolving parameters.