Casting

The dynamic object returned in response to a HTTP method supports casting to a static object.

Implicit casting

Implicit casting to a static object type:

dynamic client = new RestClient("http://jsonplaceholder.typicode.com");

User user = await client.Users.Get(1);

For convenience casting to the HttpResponseMessage is supported too:

dynamic client = new RestClient("http://jsonplaceholder.typicode.com");

HttpResponseMessage httpResponseMessage = await client.Users.Get(1);

Assert.That(httpResponseMessage.StatusCode, Is.EqualTo(HttpStatusCode.OK));

Collections

Collections either dynamically or as static object types are supported.

You can iterate over the dynamic type returned from a HTTP method:

dynamic client = new RestClient("http://jsonplaceholder.typicode.com");

var users = await client.User.Get();

foreach (var user in users)
{
   Console.Writeline(user.id);
}

Using the dynamic type returned you can also access by index:

dynamic client = new RestClient("http://jsonplaceholder.typicode.com");

var users = await client.Users.Get();

Assert.That(users[0].id, Is.EqualTo(1));

The dynamic object returned can be cast to a collection of statically typed objects. RestClient supports deserializing to same types as Json.NET IList, IEnumerable, IList<T>, Array, IDictionary, IDictionary<TKey, TValue> etc.

Example usage:

dynamic client = new RestClient("http://jsonplaceholder.typicode.com");

List<User> users = await client.Users.Get();