C#字段:必须初始化但仍可访问反序列化器。

huangapple go评论117阅读模式
英文:

C# fields: mandatory initialization but still accessible to deserializer

问题

I'm only providing the translated content without any additional information or answers:

我面临一个在C#(和其他面向对象语言)中非常常见的困境,我试图满足三个竞争性要求:

  1. 我绝对不希望我的类的最终用户在构造时忘记初始化某些字段。
  2. 我希望一个反序列化器能够实例化该类并填充字段,而不会被私有设置器“阻塞”。
  3. 我不希望留下任何未初始化的字段(例如,必须构造列表,不允许null)。

注意:
这不是与这个旧问题相同的问题: https://stackoverflow.com/questions/10794346/how-to-make-a-property-required-in-c
出于两个原因:

  1. 它没有涉及到要求 #2(暴露字段以进行反序列化)。
  2. 它已经有10年了,C#自那时以来发生了很大的变化。

所以,假设这是我的类:

  1. public class MyClass {
  2. public bool Field1 {get; set;}
  3. public IEnumerable<bool> Field2 {get; set;} = default!;
  4. }

注意:不要过于关注= default!这部分,在这里我只是让编译器高兴(它检测到未初始化的字段),以便继续核心问题。

如果我只需要满足要求 #1,那么我不会这样做:

  1. var o = new MyClass() {
  2. Field1 = true
  3. // 哎呀,我忘记Field2了
  4. };

相反,我会这样做:

  1. public class MyClass {
  2. public bool Field1 {get; private set;}
  3. public IEnumerable<bool> Field2 {get; private set;}
  4. public MyClass(bool field1, IEnumerable<bool> field2) {
  5. Field1 = field1;
  6. Field2 = field2;
  7. }
  8. }

对于只有要求 #3,我会这样做:

  1. public class MyClass {
  2. public bool Field1 {get; private set;}
  3. public IEnumerable<bool> Field2 {get; private set;} = new List<bool>();
  4. public MyClass(bool field1) {
  5. Field1 = field1;
  6. }
  7. }
  8. // ...
  9. c.Field2.AddRange(...);

不会这样做,因为尽管保护了字段免受篡改,但不能保证要求 #1

  1. public class MyClass {
  2. public bool Field1 {get; init;}
  3. public IEnumerable<bool> Field2 {get; init;} = new List<bool>();
  4. }

但现在我必须满足要求 #2。这是一个问题,因为反序列化器不能填充protectedprivate字段。

这意味着这将不会起作用,因为Field1仍然保持为false
(注意:我们假设反序列化器已正确配置:没有大小写混淆等)

  1. string json = @"{ ""Field1"": true, ""Field2"": [] }";
  2. var o = JsonSerializer.Deserialize<MyClass>(json);
  3. Assert(o.Field1 == true); // 失败

我所知道的满足所有三个要求的唯一解决方案是具有所有字段的构造函数:

  1. public class MyClass {
  2. public bool Field1 {get; private set;}
  3. public IEnumerable<bool> Field2 {get; private set;} = new List<bool>();
  4. [JsonConstructor] // <-- 使其无懈可击!
  5. public MyClass(bool field1, IEnumerable<bool> field2) {
  6. Field1 = field1;
  7. Field2 = field2;
  8. }
  9. }
  10. string json = @"{ ""Field1"": true, ""Field2"": [] }";
  11. var o = JsonSerializer.Deserialize<MyClass>(json);
  12. Assert(o.Field1 == true); // 成功

即使这个解决方案也不完美,因为如果我向类中添加一个字段“Field3”,然后可能忘记将其添加到详尽的构造函数中,反序列化会默默忽略它。失败!

我的问题:

在现代C#中是否有一种优雅的方法来实现这一点?
许多关于这个主题的答案都已有10多年了。C#在各个方向都取得了巨大的进步。理想情况下,我希望得到**.Net6(C# 10)**的答案,并且可能还有关于更近期C#(C# 11+)的答案。

英文:

I'm facing a dilemma that's very common in C# (and other OO languages) where I'm trying to meet 3 competing requirements :

> 1. I absolutely don't want the end-user of my class to forget to initialize some of the fields, at construction time.
> 2. I want a deserializer to be able to instantiate that class and populate the fields without silently being "blocked" by a private setter.
> 3. I don't want any non-initialized fields left hanging (e.g. a list has to be constructed, no loose null in there!)
>

Note:
> This is NOT the same question as this old one : https://stackoverflow.com/questions/10794346/how-to-make-a-property-required-in-c
> for two reasons :
> 1. It doesn't deal with requirement #2 (expose fields for deserialization)
> 2. It's 10 years old, and C# has evolved a lot since then.

So, let's say that this is my class :

  1. public class MyClass {
  2. public bool Field1 {get; set;}
  3. public IEnumerable&lt;bool&gt; Field2 {get; set;} = default!;
  4. }

Note : don't obsess over the = default! bit, here I'm just making the compiler happy (it detects non-initialized fields) to move on to the core of the issue.

if I needed to meet requirement #1 only then I would NOT do this :

  1. var o = new MyClass() {
  2. Field1 = true
  3. // Uh oh, I forgot Field2
  4. };

Instead I would do this :

  1. public class MyClass {
  2. public bool Field1 {get; private set;}
  3. public IEnumerable&lt;bool&gt; Field2 {get; private set;}
  4. public MyClass(bool field1, IEnumerable&lt;bool&gt; field2) {
  5. Field1 = field1;
  6. Field2 = field2;
  7. }
  8. }

For requirement #3 only I would do this :

  1. public class MyClass {
  2. public bool Field1 {get; private set;}
  3. public IEnumerable&lt;bool&gt; Field2 {get; private set;} = new List&lt;bool&gt;();
  4. public MyClass(bool field1) {
  5. Field1 = field1;
  6. }
  7. }
  8. // ...
  9. c.Field2.AddRange(...);

I would NOT do this because despite protecting the fields from tampering, it does not guarantee requirement #1 :

  1. public class MyClass {
  2. public bool Field1 {get; init;}
  3. public IEnumerable&lt;bool&gt; Field2 {get; init;} = new List&lt;bool&gt;();
  4. }

But now I have to fullfil requirement #2 . That's a problem because the deserializer cannot populate protected or private fields.

Which means that this would NOT work, as Field1 would remain false :
(note: we're assuming that the deserializer is properly configured : no upper-case/lower-case nonsense or whatnot)

  1. string json = @&quot;{ &quot;&quot;Field1&quot;&quot;: true, &quot;&quot;Field2&quot;&quot;: [] }&quot;;
  2. var o = JsonSerializer.Deserialize&lt;MyClass&gt;(json);
  3. Assert(o.Field1 == true); // fails

The only solution I'm aware of to fulfill all 3 requirements is a constructor that has ALL the fields :

  1. public class MyClass {
  2. public bool Field1 {get; private set;}
  3. public IEnumerable&lt;bool&gt; Field2 {get; private set;} = new List&lt;bool&gt;();
  4. [JsonConstructor] // &lt;-- to make it air-tight!
  5. public MyClass(bool field1, IEnumerable&lt;bool&gt; field2) {
  6. Field1 = field1;
  7. Field2 = field2;
  8. }
  9. }
  10. string json = @&quot;{ &quot;&quot;Field1&quot;&quot;: true, &quot;&quot;Field2&quot;&quot;: [] }&quot;;
  11. var o = JsonSerializer.Deserialize&lt;MyClass&gt;(json);
  12. Assert(o.Field1 == true); // succeeds

even that solution is not perfect, because if I add a field "Field3" to the class then I might forget to add it to the exhaustive constructor, and the deserialization would silently ignore it. Fail!

My question :

Is there an elegant way of achieving this in modern C#?
Many answers about this topic are 10+ years old. C# has made a ton of progress in every direction since then.
Ideally, I'd like an answer for .Net6 (C# 10) AND possibly an answer for more recent C# (C# 11+)

答案1

得分: 1

For C# 11 - there is feature introduced to specifically support such scenarios - required modifier which allows your to do something like (also note the usage of init keyword which declares an init-only setter which allows to assign a value to the property only during object construction):

  1. public class MyClass
  2. {
  3. public required bool Field1 {get; init;}
  4. public required IEnumerable&lt;bool&gt; Field2 {get; init;}
  5. }

The last option can also be reduced to using records (available since C# 9):

  1. public record MyClass(bool Field1, IEnumerable&lt;bool&gt; Field2);

P.S.

Note that required modifier is also considered by System.Text.Json, so marking property with it will make the corresponding JSON property required in the JSON payload.

英文:

For C# 11 - there is feature introduced to specifically support such scenarios - required modifier which allows your to do something like (also note the usage of init keyword which declares an init-only setter which allows to assign a value to the property only during object construction):

  1. public class MyClass
  2. {
  3. public required bool Field1 {get; init;}
  4. public required IEnumerable&lt;bool&gt; Field2 {get; init;}
  5. }

The last option can also be reduced to using records (available since C# 9):

  1. public record MyClass(bool Field1, IEnumerable&lt;bool&gt; Field2);

P.S.

Note that required modifier is also considered by System.Text.Json, so marking property with it will make the corresponding JSON property required in the JSON payload.

huangapple
  • 本文由 发表于 2023年5月17日 17:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定