具有只读成员的可序列化对象?

时间:2019-03-29 19:18:53

标签: c# serialization

我有这组代码,但是我真正想要的是公开x和y只读,或者只公开暴露于getter的公开。通常,在C ++中,您会将序列化器作为朋友类。 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);
    }
}

0 个答案:

没有答案