Replies: 2 comments 5 replies
-
[System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public Child()
{
A = 12;
} |
Beta Was this translation helpful? Give feedback.
-
That is correct. An earlier version allowed for more complex control, but we decided to simplify for now.
No, this has never and will never be on the table. Control flow analysis is too easily fooled or confused for use in determining the public api of anything. For example, if you have multiple constructors and refactor a helper method for setting common things between those constructors, the analysis now misses whatever was set in that helper. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am using latest VS 2022 17.3 Preview 2. According to Language Feature Status the bits with required members support are already in VS 17.3 P2, and I can use the new
required
keyword just fine.However, I was wondering if it's by design or a bug if I do not set the required member explicitly in the derived class instantiation (because it is already set explicitly within the derived class's constructor):
So, why does it complain with CS9035? Required property
A
is set explicitly in theChild
's constructor, why the need for me to also set it in my user code when I instantiate the child class?UPDATE:
Answer was provided by @ufcpp to use
[System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
attribute on the constructor. Which works great, however this raises another issue...As per docs from
SetsRequiredMembersAttribute
and from the design:Does it therefore mean that should the above scenario be made a bit more complex:
... that it is not possible currently to selectively set required members? That is: it seems that currently there is only "all" or "none" mechanism to specify which required members have been set by a constructor.
Would it not have been better to have performed simple recursive analysis, progressively looking at constructor chain to determine which required members have been definitely assigned, and which have not?
Such more fine-grained control would:
SetsRequiredMembers
attributeAny reasons why this approach have not been taken, and will it be looked at in the future?
Beta Was this translation helpful? Give feedback.
All reactions