Facebook C # SDK-.NET 3.5 and dynamic objects

I downloaded the Graph C # SDK for facebook, the examples are very useful and understandable, but I am untying when I try to use the dynamic type of the object as a return object to call FacebookApp.Get ("I").

I never used the dynamics, so I tried a little, and it seems that they are new for Visual Studio 2010, this is the version that I use, but I can not use the latest structure because of my working environment ...

Can I use a type that I can use, or is there any other way to do this using the 3.5 framework? here is an example from source files provided with binary code for the framework.

dynamic myInfo = app.Get("me"); lblTitle.Text = myInfo.name; 

I get an error message that might not have a link assembly for the dynamic type.

Any help is greatly appreciated! Looking forward to handle this SDK!

+4
source share
4 answers

If you need strongly typed objects, this is a very simple way to do this. 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; } } 
+5
source

You can see 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 pass it to IDictionary <string, object>

+4
source

Based on some other answers here, dynamic objects are really JSON objects that are returned from the facebook api. The SDK uses a dynamic type to create a more convenient interface with basic data.

I did not like the idea of ​​casting objects in an IDictionary every time, so I took another step and created a facade object that provides a strongly typed data access method.

 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"); } } } 
+1
source

dynamic is C # 4.0.Net 4.0 not.Net 3.5

-1
source

Source: https://habr.com/ru/post/1335324/


All Articles