Well, I tried it successfully. The only thing I've had to change was
the ParseClass method since it was only able to fill fields, not
properties. I changed it to the following and now it works at least
the way I like it. Maybe someone should add a little more error
handling. :)
Quick & dirty:
private static object ParseClass(Type toType, object toCast)
{
// parse and return
//
object ret = Activator.CreateInstance(toType);
Dictionary<string, object> val = toCast as
Dictionary<string, object>;
try
{
FieldInfo[] fields = toType.GetFields();
PropertyInfo[] properties = toType.GetProperties();
foreach (FieldInfo fi in fields)
{
string key = fi.Name.ToLower();
if (ContainsKey(val, key))
{
// we found the value
fi.SetValue(ret, GetValue(val, key));
}
else
{
throw new Exception("The cast is not valid.
Field name: " + fi.Name + " does not exist in the XmlResponse");
}
}
foreach(PropertyInfo pr in properties)
{
string key = pr.Name.ToLower();
if(ContainsKey(val, key))
{
pr.SetValue(ret, GetValue(val, key), null);
}
else
{
throw new Exception("The cast is not valid.
Property name: " + pr.Name + " does not exist in the XmlResponse");
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Parse error: " +
e.Message);
ret = null;
}
return ret;
}
On Jul 25, 4:24 pm, Uday Verma <
uday.ka...@gmail.com> wrote:
> Yeah, I think so. Please don't take my word for it though. I haven't
> worked with the lib in a while now, and am just maintaining it here and
> there.
> I think you can try casting this response into a List<YouClass> type .. I
> think that should work, in case it doesn't let me know, because I think it
> is supposed to.
>
> If you have mixed types in your response, which I think you don't, you can
> try casting to List<object> and do your own introspection and casting.
>
> Cheers,
>