TryParse arrays of struct

10 views
Skip to first unread message

SeveQ

unread,
Jul 25, 2009, 9:03:03 AM7/25/09
to xmlrpc-silverlight
Hello there,

just trying your lib. Works great so far. But one thing I'd like to
ask: is it anyhow possible to TryCast an array of struct objects into
a .NET array of class objects?

I'd like to cast something like

<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><array>
<data>
<value><struct>
<member><name>Filename</name>
<value><string>testfile.txt</string></value>
</member>
<member><name>Filesize</name>
<value><int>123</int></value>
</member>
</struct></value>
<value><struct>
<member><name>Filename</name>
<value><string>blahblubbfile.txt</string></value>
</member>
<member><name>Filesize</name>
<value><int>123</int></value>
</member>
</struct></value>
<value><struct>
<member><name>Filename</name>
<value><string>blahblahfile.txt</string></value>
</member>
<member><name>Filesize</name>
<value><int>123</int></value>
</member>
</struct></value>
</data>
</array></value>
</param>
</params>
</methodResponse>

into an array of .NET objects that each has the properties Filename
and Filesize. Is your lib already capable of doing this?

Thanks!

Hendrik

Uday Verma

unread,
Jul 25, 2009, 10:24:08 AM7/25/09
to xmlrpc-si...@googlegroups.com
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,

SeveQ

unread,
Jul 25, 2009, 11:08:59 AM7/25/09
to xmlrpc-silverlight
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,
>

Uday Verma

unread,
Jul 25, 2009, 11:16:50 AM7/25/09
to xmlrpc-si...@googlegroups.com
Nice! .. looks great.  If you want, I can give you write access to the repository and you can check in the changes?
--
Uday
http://soundc.de/

SeveQ

unread,
Jul 25, 2009, 12:25:15 PM7/25/09
to xmlrpc-silverlight
Yeah, sure. Can't promise I'll make it today, but I'll see what I can
do.


On Jul 25, 5:16 pm, Uday Verma <uday.ka...@gmail.com> wrote:
> Nice! .. looks great.  If you want, I can give you write access to the
> repository and you can check in the changes?
>

Uday Verma

unread,
Jul 29, 2009, 2:42:09 PM7/29/09
to xmlrpc-si...@googlegroups.com
Sorry took me a while to get back to you.  Do you have some sort of google credentials I can use to make you a contributor on the project?
--
Uday
http://soundc.de/

SeveQ

unread,
Jul 29, 2009, 3:34:17 PM7/29/09
to xmlrpc-silverlight
That's okay. It's not my top priority project. Actually, it's just a
hobby. Nothing commercial, nothing a customer waits for. So...
doesn't matter. What kind of credentials do you need?

On Jul 29, 8:42 pm, Uday Verma <uday.ka...@gmail.com> wrote:
> Sorry took me a while to get back to you.  Do you have some sort of google
> credentials I can use to make you a contributor on the project?
>

Uday Verma

unread,
Jul 30, 2009, 10:50:31 AM7/30/09
to xmlrpc-si...@googlegroups.com
That's cool, you can take your time to check in the changes, no rush.

I don't know, some kind of an id, usually a gmail address or something that you can use to checkout the code in read/write mode, it would be the credentials you use to use google services I guess.
--
Uday
http://soundc.de/

SeveQ

unread,
Aug 17, 2009, 4:48:03 AM8/17/09
to xmlrpc-silverlight
Okay, I've had some other things to do as well. Here I am again.

Well, to login into Google I basically use the email address I also
use in this group: webm...@syncro-community.de. So I guess you can
use it to make me a member of the project.





On 30 Jul., 16:50, Uday Verma <uday.ka...@gmail.com> wrote:
> That's cool, you can take your time to check in the changes, no rush.
>
> I don't know, some kind of an id, usually a gmail address or something that
> you can use to checkout the code in read/write mode, it would be the
> credentials you use to use google services I guess.
>

Uday Verma

unread,
Aug 17, 2009, 11:08:56 AM8/17/09
to xmlrpc-si...@googlegroups.com
great! .. I have added you as a project owner.
--
Uday
http://soundc.de/

SeveQ

unread,
Aug 17, 2009, 12:06:44 PM8/17/09
to xmlrpc-silverlight
Thanks a lot. Have already checked in my changes (r15).

On 17 Aug., 17:08, Uday Verma <uday.ka...@gmail.com> wrote:
> great! .. I have added you as a project owner.
>
>
>
> On Mon, Aug 17, 2009 at 3:48 AM, SeveQ<webmas...@syncro-community.de> wrote:
>
> > Okay, I've had some other things to do as well. Here I am again.
>
> > Well, to login into Google I basically use the email address I also
> > use in this group: webmas...@syncro-community.de. So I guess you can
Reply all
Reply to author
Forward
0 new messages