Deserialize json into C# object for class which has default private constructor

前端 未结 4 1646
误落风尘
误落风尘 2020-12-14 19:03

I need to deserialize json for following class.

public class Test
{
    public string Property { get; set; }

    private Test()
    {
        //NOTHING TO I         


        
4条回答
  •  再見小時候
    2020-12-14 19:38

    It doesn't seem like you need to take any extra steps.

    Using Json.NET v6.0.8, the following C# program works inside LINQPad:

    void Main()
    {   
        var o = JsonConvert.DeserializeObject("{\"Property\":\"Instance\"}");
    
        Debug.Assert(o.Property == "Instance",
            "Property value not set when deserializing.");
    }
    
    public class Test
    {
        public string Property { get; set; }
    
        private Test()
        {
        }
    
        public Test(string propertyValue)
        {
            Property = propertyValue;
        }
    }
    

提交回复
热议问题