0

I have this set of code, but what I'd really like is to make x and y readonly public, or public only with a getter exposed. Normally in C++ you'd make the serializer a friend class. Is there an equivalent trick in C#?

public struct z
{
    public int x;
    public int y;
    public z(int a, int b)
    {
        x = a;
        y = b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<z> b = new List<z>();
        b.Add(new z(3, 4));
        System.Xml.Serialization.XmlSerializer w  = new System.Xml.Serialization.XmlSerializer(typeof(List<z>));
        System.IO.StringWriter x = new System.IO.StringWriter();
        w.Serialize(x,b);
        System.IO.StringReader y = new System.IO.StringReader(x.ToString());
        List<z> c = (List<z>)w.Deserialize(y);
        Console.WriteLine(b[0].x);
        Console.WriteLine(c[0].x);
    }
}
Carbon
  • 3,828
  • 3
  • 24
  • 51
  • public int x { get; } – Davide Vitali Mar 29 '19 at 19:39
  • Ah, oh, ok. So get and sets aren't really methods (eg, not invoked with `()` but something different? – Carbon Mar 29 '19 at 20:12
  • getters and setters from [MSDN](https://learn.microsoft.com/en-US/dotnet/csharp/programming-guide/classes-and-structs/using-properties) – Davide Vitali Mar 29 '19 at 20:14
  • Wait, then the serialization still doesn't work. – Carbon Mar 29 '19 at 20:21
  • Why doesn't this give a compiler error or warn? – Carbon Mar 29 '19 at 20:23
  • What error are you getting? – Davide Vitali Mar 29 '19 at 20:40
  • Because `List<>` implements `ICollection`, you need to set your class to use private fields and public properties, as public properties are not going to be serialized. To keep things simple you could use an array instead. More on [official documentation](https://learn.microsoft.com/en-US/dotnet/standard/serialization/examples-of-xml-serialization#serializing-a-class-that-contains-a-field-returning-a-complex-object) – Davide Vitali Mar 29 '19 at 20:55
  • List<> is O(1) insert and array is O(n)? – Carbon Mar 29 '19 at 21:09
  • Is this the equivalent of a StandardLayoutType which can be memcpy'd vs a non-StandardLayoutType? – Carbon Mar 29 '19 at 21:11
  • there is an interesting QA on another stack overflow site about that. I only know some basic instructions about C/C++ so I can’t say about the comparison against StandardLayoutType you’ve mentioned. [Link](https://softwareengineering.stackexchange.com/questions/221892/should-i-use-a-list-or-an-array) bottom line, best performance depends on what you need to do with your collection. Fixed length (or at least not varying much)? Go for Array. Variable size? Go for List<> – Davide Vitali Mar 29 '19 at 22:12
  • There is no equivalent of the c++ `friend class` concept in C#. So any standard deserializer won't be able to write on public properties, which only expose a getter. And it surely won't be able to write on private or protected memers. – derpirscher Mar 30 '19 at 01:17
  • Closest equivalent would be to replace the `z` struct with a DTO that supports implicit conversion from and to the "real" type, see [Most elegant XML serialization of Color structure](https://stackoverflow.com/q/3280362/3744182). But other serializers support different mechanisms. – dbc Mar 30 '19 at 03:36

0 Answers0