Reputation: 163
I have defined a ParseObject subclass called LeaderboardScore, which is returned from my cloud code function as an IDictionary<string, object>
.
I was hoping that I could just do something like in the example below, but the cast fails :(
Failed attempt to cast:
ParseCloud.CallFunctionAsync<IDictionary<string, object>>("getScore", parameters).ContinueWith(t =>
{
LeaderboardScore score = t.result as LeaderboardScore;
Debug.Log(score.get<string>("facebookId"));
}
LeaderboardScore Definition:
[ParseClassName("LeaderboardScore")]
public class LeaderboardScore : ParseObject
{
[ParseFieldName("facebookId")]
public string FacebookId
{
get { return GetProperty<string>("FacebookId"); }
set { SetProperty<string>(value, "FacebookId"); }
}
[ParseFieldName("score")]
public int Score
{
get { return GetProperty<int>("Score"); }
set { SetProperty<int>(value, "Score"); }
}
}
Note that t.Result does have the correct information, meaning I can access it by calling things like t.Result["facebookId"] as string
, but it would be much nicer to be able to pass around a LeaderboardScore object instead of an IDictionary<string, object>
.
If anyone could shed some light on this issue I would greatly appreciate it! :)
Upvotes: 0
Views: 695
Reputation: 163
The correct way to do this is to create native subclasses for each of the ParseObjects you are interested in, then having your cloud function return that type or list of that type like so:
ParseCloud.CallFunctionAsync<LeaderboardScore>("getScore", parameters).ContinueWith(t =>
{
LeaderboardScore score = t.Result;
}
Parse takes care of the conversion so you don't have to worry about it. To return a list, simply use IList<LeaderboardScore>
Upvotes: 0
Reputation: 21
You can cast all dictionary to object(with property) by:
public static T ToObject<T>(this IDictionary<string, object> source)
where T : class, new()
{
T someObject = new T();
Type someObjectType = someObject.GetType();
foreach (KeyValuePair<string, object> item in source)
{
someObjectType.GetProperty(item.Key).SetValue(someObject, item.Value, null);
}
return someObject;
}
Upvotes: 1