2

我只是在开发一个简单的程序,该程序使用 RestSharp 将 POST 方法发送到启用运动轴的机器。

var client = new RestClient("http://10.101.2.216/");
var request = new RestRequest("/api/task/library/command", Method.POST);
request.AddBody(new { command = "Enable X"});

...

我想知道最后一行到底发生了什么。创建什么类型的变量?编译器实际上在做什么?换句话说,它是如何转换成 JSON 的,为什么它会起作用?

4

1 回答 1

4

C# 4.0 introduce anonymous types. Those types aren't defined by any specific type. To construct anonymous type we use the new { ... } syntax. What RestSharp is actually doing it's using Reflection to serialize the type into JSON or XML.

于 2013-05-29T13:37:39.353 回答