0

Is it possible to inform resharper that a class will be deserialized, in order to stop it suggesting "Auto-property can be made get-only"? Especially since accepting the resharper suggestion to make the property get-only will break deserialization using System.Text.Json.

Below is an example scenario, where we have an initialized list whose contents are updated and subsequently serialized. The setter initializer is required so that the object can be deserialized via System.Text.Json.

public class MyClass
{
    public List<int> Numbers { get; set; } = new(); // resharper warns property can be made get only
}

I appreciate this can be done explicitly for each property (or the entire file) via a 'disable comment', but this is rather ugly. // ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global

stoj
  • 1,116
  • 1
  • 14
  • 25
  • Just remove new(), you should always assume that any property is not initialized – Serge Mar 26 '23 at 12:52
  • Why should properties be assumed as unitialized? Especially collections, where it's good practise they are initialized. https://stackoverflow.com/questions/1969993/is-it-better-to-return-null-or-empty-collection – stoj Mar 26 '23 at 13:05
  • Otherwise to be sure that this property was not set to null somewhere, remove set as Resharpers advise or replace it with init – Serge Mar 26 '23 at 14:35
  • Unfortunately the setter can't be removed (or replaced with an init) as this will break the (json) deserialization.. which is the crux of my question. FYI, I'm also aware this can be worked around by providing a constructor with parameters matchign the property names, but this in my view is an even uglier (and more brittle) solution. Fortunately, since posting the question i have located the answer I needed and will post it below. – stoj Mar 26 '23 at 15:00

2 Answers2

0

A trivial fix in the end.. assigning the class as Serializable is sufficient for ReSharper to correctly identify that the class may be deserialized. And thus the property setter (or initalizer) is actually required, i.e. no longer suggests making the property 'get only'.

[Serializable] // resharper is now aware that the class may be deserialized and no longer suggests the property can be made 'get only'
public class MyClass
{
    public List<int> Numbers { get; set; } = new();
}
stoj
  • 1,116
  • 1
  • 14
  • 25
0

Newtonsof.Json will deserialize even without set; But if you decided to go the hard way and using Text.Json, use init; instead of set; It would be good not only to deserialize using Text.Json but to prevent the NullReference exceptions too.

using System.Text.Json;

public class MyClass
{
   public List<int> Numbers{ get; init; } = new();
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Agreed, this is n/a for Newtonsoft.Json (unlike Text.Json) since it doesn't require a setter during deserialization. But my question was regarding Text.Json (which I'm using because it is substantially faster). I'll update the question to make this more explicit. – stoj Mar 27 '23 at 08:16