JSON deserialize to constructed protected setter array

后端 未结 2 1193
独厮守ぢ
独厮守ぢ 2020-12-11 15:29

I use Newtonsoft JSON to serialize/deserialize my objects. One of those contains an array with a protected setter because the constructor build the array itself and only the

2条回答
  •  旧时难觅i
    2020-12-11 16:05

    Mark Outs with the [JsonProperty] attribute:

        private class Engine
        {
            public string Text { get; set; }
            public int Id { get; set; }
            [JsonProperty]  // Causes the protected setter to be called on deserialization.
            public Coords[] Outs { get; protected set; }
    
            public Engine()
            {
                this.Outs = new Coords[3];
                for (int i = 0; i < this.Outs.Length; i++)
                {
                    this.Outs[i] = new Coords();
                }
            }
        }
    

提交回复
热议问题