Suppose I write the following C# code to define a web method:
public class Thing
{
public string X {get;set}
public string Y {get;set}
}
[WebMethod]
public static void myFunction(Thing thing) { }
I’ve discovered that I can invoke the function using the jQuery JavaScript that looks like this:
var myData = { X: "hello", Y: "world" };
var jsonData = JSON.stringify(myData);
jQuery.ajax({ data: jsonData, ...
When myFunction is thus invoked, thing.X is set to “hello” and thing.Y is set to “world”. What exactly does the .net framework do to set the value of thing? Does it invoke a constructor?
Just like you can create Thing like so
So no it does not call the constructor to answer your question.
Ok, more detail…
It takes the JSON and deserializes it. It fills the properties from you JSON object. For example, if you had the following in JSON:
The serializer would not find X or Y for thing and set them to the default value for that data type.
Let’s say you want to go deeper. You can custom serialize and deserialize your objects:
So you can see, it’s fishing for parameters in your case,
XandY.