Allowing private fields initialization in constructor with System.Text.Json #55036
-
Hi, Seems that @vintzl failed to explain what we want. I have added a repro here. Ultimately, we just want to know what you can do to get private fields populated in the constructor like stated in the repro. Thank you for your attention. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
Hello, I have been following along on the other threads, wherein @vintzl has been quite disrespectful to @eiriktsarpalis. I have hidden the comment that @vintzl made here too, as it was disruptive. While the repro added here captures the scenario described in a standalone manner, it doesn't change the answer or guidance that can be provided. For that reason, I am going to lock this conversation and reply back on #54984. -Jeff |
Beta Was this translation helpful? Give feedback.
-
Quoting your document here, for easier reading:
You can make it work but you need to have public properties. Serialization, by its nature, is part of your public API. Serialization needs to be robust. Code like this seems very suspicious to me: public class Test
{
private readonly ushort _id;
private readonly string _name;
public Test(ushort id, string name)
{
_id = id;
_name = name;
}
} Did the author rally expect to have those fields filled in deserialization? It seems very odd to allow fields to be deserialized that can't be serialized. If you don't like to see the public class Test
{
public Test(ushort id, string name)
{
Id = id;
Name = name;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public ushort Id { get; }
[EditorBrowsable(EditorBrowsableState.Never)]
public string Name { get; }
} |
Beta Was this translation helpful? Give feedback.
@mickturn
Quoting your document here, for easier reading: