I don't think RestSharp needs to introduce new method. Why don't you just
make it work for client.Execute<dynamic> or client.Execute<object>
Here is how you would do for strongly typed currently.
var client = new RestClient("https://graph.facebook.com");
var result = client.Execute<User>(new RestRequest("4"));
var user = result.Data;
string name = user.name;
string id = user.id;
So make it dynamic if T == typeof(object) and response.content-type ==
"application/json",
In simple json if you pass <T> as System.Object or dynamic, it will just
create either jsonobject/jsonarry/bool/long/string and other primitive
types. So you can use dynamic.
string json = "{\"id\":\"4\",\"name\":\"Mark Zuckerberg\",\"first_name\":\"Mark\",\"last_name\":\"Zuckerberg\",\"link\": \"http:\\/\\/www.facebook.com\\/zuck\",\"username\":\"zuck\",\"gender\":\"male\",\"locale\":\"en_US\"}";
var stronglyTypeObject = SimpleJson.DeserializeObject<User>(json);
var stornglyTypedName = stronglyTypeObject.name;
dynamic dynamicObject = SimpleJson.DeserializeObject<dynamic>(json);
var dynamicObjectName = dynamicObject.name;
dynamic jsonObject = SimpleJson.DeserializeObject<JsonObject>(json);
var josnObjectName = jsonObject.name;
dynamic obj = SimpleJson.DeserializeObject<object>(json);
var objName = obj.name;
and .net 3.5/wp7 users where dynamic is not supported can still continue to
use Execute<object> but instead of dynamic since it returns
JsonObject/JsonArray so you will have to cast it to
IDictionary<string,object> or IList<object>
var dict = SimpleJson.DeserializeObject<object>(json) as IDictionary<string, object>;
var dictName = obj["name"];
Nathan Totten has a blog about this on how we solve the dynamic craziness
in various .net platforms/versions in Facebook C# SDK
http://blog.ntotten.com/2010/09/07/dynamic-objects-and-the-facebook-c...
Here is some extra points on how we deal with FB C# SDK.
By default when you install SimpleJson from nuget, JsonArray and JsonObject
classes are public but hidden from intellisense using
[EditorBrowsable(EditorBrowsableState.Never)]. These two classes are the
main feature that allows you to have dynamic. You need to
define SIMPLE_JSON_DYNAMIC conditional symbol though. If you use the
default json serializer provided by the FB C# SDK which is SimpleJson you
get dynamic support out of the box. But if you want to use other json
serializers such as json.net we provide a wrapper which converts JArray to
JsonArray and JObject to JsonObject. Full code can be found at
https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/blob/a914d...
In the end this will look better for those that support dynamic.
var client = new RestClient("https://graph.facebook.com");
var result = client.Execute<dynamic>(new RestRequest("4"));
var user = result.Data;
string name = user.name;
string id = user.id;
And for those unlucky .net 3.5 and wp7 devs where dynamic is not supported
you still partially benefit from it.
var client = new RestClient("https://graph.facebook.com");
var result = client.Execute<object>(new RestRequest("4"));
var user = (IDictionary<string, object>)result.Data;
var name = (string)user["name"];
var id = (string)user["user"];
On Tuesday, April 17, 2012 8:14:52 PM UTC-4, John Sheehan wrote:
> public static partial class RestClientExtensions
> {
> public static RestResponse<dynamic> ExecuteDynamic(this IRestClient
> client, IRestRequest request)
> {
> var response = client.Execute(request);
> var generic = (RestResponse<dynamic>)response;
> dynamic content = SimpleJson.DeserializeObject(response.Content);
> generic.Data = content;
> return generic;
> }
> }