How to pass arguments to a non-default constructor?

Nov 24, 2011 at 8:16 AM
Edited Nov 24, 2011 at 8:17 AM

This is a copy of StackOverflow post

Hi there! I have approximately the following picture:

 

    public class Foo
    {
       public Foo(Bar bar, String x, String y)
       {
           this.Bar = bar;
           this.X = x;
           this.Y = y;
       }

       [JsonIgnore]
       public Bar Bar { get; private set; }

       public String X { get; private set; }
       public String Y { get; private set; }
    }

    public class Bar
    {
        public Bar(String z)
        {
            this.Z = z;
        }

        public String Z { get; private set; }
    }

 

I want somehow to pass an object of type Bar to a constructor of type Foo during deserialization, i.e:

    var bar = new Bar("Hello world");
    var x = JsonConvert.DeserializeObject(fooJsonString, bar);