Skip to content

Instantly share code, notes, and snippets.

@Clancey
Last active December 18, 2015 04:08
Show Gist options
  • Select an option

  • Save Clancey/5723079 to your computer and use it in GitHub Desktop.

Select an option

Save Clancey/5723079 to your computer and use it in GitHub Desktop.
Parse helper class
/* Sample object
public class EventModel
{
[DataMember(Name = "description")]
public string Description {get;set;}
[DataMember(Name = "endTime")]
public DateTime EndTime {get;set;}
[DataMember(Name = "location")]
public string Location {get;set;}
[DataMember(Name = "rating")]
public int Rating {get;set;}
[DataMember(Name = "ratingCount")]
public int RatingCount { get; set; }
[DataMember(Name = "ratingSum")]
public int RatingSum {get;set;}
[Ignore,DataMember(Name="speaker")]
public List<object> parseSpeakers {get;set;}
public List<Speaker> Speakers
{
get{ return parseSpeakers.Select (x => (x as ParseObject).ToObject<Speaker> ()).ToList ();}
}
[DataMember(Name = "startTime")]
public DateTime StartTime {get;set;}
[DataMember(Name = "title")]
public string Title { get; set; }
}
*/
using System;
using MonoTouch.Foundation;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Reflection;
using System.Collections.Generic;
namespace Parse
{
public static class ParseHelper
{
public static ParseObject ToPfObject (this object obj)
{
var type = obj.GetType ();
return obj.ToPfObject (type.Name);
}
public static ParseObject ToPfObject (this object obj, string className)
{
try {
var pfObj = new ParseObject(className);
obj.UpdateParseObject(ref pfObj);
return pfObj;
} catch (Exception e) {
Console.WriteLine (e);
return null;
}
}
public static T ToObject<T> (this ParseObject pfObj) where T : new ()
{
T obj = new T ();
var type = obj.GetType ();
var keys = pfObj.Keys;
foreach (var prop in type.GetProperties()) {
try {
if (Attribute.IsDefined (prop, typeof(IgnoreDataMemberAttribute)))
continue;
string keyName = Attribute.IsDefined (prop, typeof(DataMemberAttribute)) ? ((DataMemberAttribute)Attribute.GetCustomAttribute (prop, typeof(DataMemberAttribute))).Name : prop.Name;
if (!keys.Contains (keyName) && keyName != "UpdatedAt" && keyName != "CreatedAt" && keyName != "ObjectId")
continue;
if (keyName == "UpdatedAt") {
prop.SetValue (obj, (DateTime)(pfObj.UpdatedAt ?? DateTime.MinValue), null);
continue;
} else if (keyName == "CreatedAt") {
prop.SetValue (obj, (DateTime)(pfObj.CreatedAt ?? DateTime.MinValue), null);
continue;
} else if (keyName == "ObjectId") {
prop.SetValue (obj, pfObj.ObjectId, null);
continue;
}
MethodInfo method = typeof(ParseObject).GetMethod("Get");
MethodInfo generic = method.MakeGenericMethod(prop.PropertyType);
var value = generic.Invoke(pfObj, new object[]{keyName});
prop.SetValue(obj,value,null);
} catch (Exception e) {
Console.WriteLine (e);
}
}
return obj;
}
public static void UpdateParseObject (this object obj, ref ParseObject pfObj)
{
var type = obj.GetType ();
if(pfObj == null)
pfObj = new ParseObject(type.Name);
var properties = type.GetProperties ();
string keyName;
foreach (var prop in properties) {
try {
if (Attribute.IsDefined (prop, typeof(IgnoreDataMemberAttribute)))
continue;
keyName = Attribute.IsDefined (prop, typeof(DataMemberAttribute)) ? ((DataMemberAttribute)Attribute.GetCustomAttribute (prop, typeof(DataMemberAttribute))).Name : prop.Name;
if (keyName == "UpdatedAt" || keyName == "CreatedAt" || keyName == "ObjectId")
continue;
var val = prop.GetValue (obj, null);
if (val == null) {
continue;
}
if(pfObj.ContainsKey(keyName))
pfObj[keyName] = val;
else
pfObj.Add(keyName,val);
} catch (Exception ex) {
Console.WriteLine (ex);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment