[英]What is an elegant way to convert IDictionary<string, object> to IDictionary<string, string>
[英]How to convert IDictionary<object,string> to ParseObject in Unity?
我定义了一个称为LeaderboardScore的ParseObject子类,该子类从我的云代码函数作为IDictionary<string, object>
。
我希望我可以像下面的例子那样做,但是强制转换失败:(
尝试投放失败:
ParseCloud.CallFunctionAsync<IDictionary<string, object>>("getScore", parameters).ContinueWith(t =>
{
LeaderboardScore score = t.result as LeaderboardScore;
Debug.Log(score.get<string>("facebookId"));
}
LeaderboardScore定义:
[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"); }
}
}
请注意,t.Result确实具有正确的信息,这意味着我可以通过将诸如t.Result["facebookId"] as string
,但是能够绕过LeaderboardScore对象而不是IDictionary<string, object>
会更好。 IDictionary<string, object>
。
如果有人可以阐明这个问题,我将不胜感激! :)
您可以通过以下方式将所有字典转换为具有属性的对象:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.