I have downloaded the Graph C# SDK for facebook, the examples are very helpful and easy to understand however i come unstuck when trying to use the dynamic object type as the re
dynamic is C# 4.0 .Net 4.0 not .Net 3.5
Building on some of the other answers here, the dynamic objects are really JSON objects that are returned from the facebook api. The SDK uses dynamic type to create a nicer interface to the underlying data.
I didn't like the idea of casting the objects to IDictionary each time so I took it a step further and created a facade object that provides a strongly-typed access method to the data.
public class FBPerson : FBBase
{
#region constructor
public FBPerson(object personObject)
: base(personObject)
{
}
#endregion
#region Properties
public string first_name
{
get { return ExtractValueAsString("first_name"); }
}
public string last_name
{
get { return ExtractValueAsString("last_name"); }
}
public string name
{
get { return ExtractValueAsString("name"); }
}
public string email
{
get { return ExtractValueAsString("email"); }
}
public string id
{
get { return ExtractValueAsString("id"); }
}
public string link
{
get { return ExtractValueAsString("link"); }
}
public string username
{
get { return ExtractValueAsString("username"); }
}
public string location
{
get { return ExtractValueAsString("location"); }
}
public string gender
{
get { return ExtractValueAsString("gender"); }
}
public string timezone
{
get { return ExtractValueAsString("timezone"); }
}
public string locale
{
get { return ExtractValueAsString("locale"); }
}
public string verified
{
get { return ExtractValueAsString("verified"); }
}
public string updated_time
{
get { return ExtractValueAsString("updated_time"); }
}
#endregion
}
And the base class (so you can create facades for other SDK objects)...
public class FBBase
{
private IDictionary<string, object> fbCollection = null;
public FBBase(object collection)
{
fbCollection = (IDictionary<string, object>)collection;
}
protected string ExtractValueAsString(string value)
{
Validate();
return fbCollection[value].ToString();
}
protected void Validate()
{
if (fbCollection == null)
{
throw new InvalidOperationException("null collection object");
}
}
}
If you want strongly typed objects there is a very easy way to do that. See here: https://gist.github.com/906471
var fb = new FacebookClient("access_token");
var result = fb.Get<FBUser>("/me");
string name = result.Name;
MessageBox.Show("Hi " + name);
[DataContract]
public class FBUser {
[DataMember(Name="name")]
public string Name { get; set; }
[DataMember(Name="first_name")]
public string FirstName { get; set; }
}
You might want to look at this article http://blog.prabir.me/post/Facebook-CSharp-SDK-Writing-your-first-Facebook-Application.aspx
var fb = new FacebookClient("access_token");
var result = (IDictionary<string, object>)fb.Get("/me");
var name = (string)result["name"];
MessageBox.Show("Hi " + name);
You will need to cast it to IDictionary<string, object>