问题
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 return object for a FacebookApp.Get("me") call.
I'v never used dynamics before so i did a bit of reasearch and it seems they are new to Visual Studio 2010, which is the version i am using but i cant use the latest framework because of my production environment...
Is there a type i can use instead or is there a different way to go about this using the 3.5 framework? here is an example from the source files provided with the binary for the framework.
dynamic myInfo = app.Get("me");
lblTitle.Text = myInfo.name;
I get an error stating that i may be missing an assembely reference for type dynamic.
Any and all help is greatly apreciated! looking forward to getting to grips with this SDK!!
回答1:
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; }
}
回答2:
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>
回答3:
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");
}
}
}
回答4:
dynamic is C# 4.0 .Net 4.0 not .Net 3.5
来源:https://stackoverflow.com/questions/4674669/facebook-c-sharp-sdk-net-3-5-dynamic-objects