Foreach + Reflexion to loop every class's property don't work
Hello ! I would like to send class's data to a PHP script using POST, I'm actually doing it by using the WWWForm class but the point is that I would like to avoid typing each single value I would like to send, this is what I'm doing right now: SortedList arguments = new SortedList (); if (contactToMod.isAbove18) arguments.Add("is_above_18", "1"); else arguments.Add("is_above_18", "0");
if (contactToMod.isAccompagnated)
arguments.Add("is_accompagnated", "1");
else
arguments.Add("is_accompagnated", "0");
// There is a lot more lines, I've just cut it to don't make the post too long
arguments.Add("notes", contactToMod.Notes);
arguments.Add("children", contactToMod.Children);
arguments.Add("idContactsDb", contactToMod.idContactsDb);
arguments.Add("idToday", contactToMod.idToday);
dummy.StartCoroutine(DoResquest(arguments, URL, callbackSuccess, callbackError));
But I found an interesting code on internet involving System.Reflection, and on a previous Unity's forum post, it seems to work well, but when I translated it in C# it didn't found any class's property (whenever my properties are all publics, but not statics) :
SortedList<string, string> argsPosted = new SortedList<string, string>();
Debug.Log ("contactToMod.Firstname = " + contactToMod.Firstname);
foreach (PropertyInfo prop in contactToMod.GetType().GetProperties())
{
argsPosted.Add(prop.Name, prop.GetValue(contactToMod, null).ToString());
Debug.Log("name = " + prop.Name + " and value = " + prop.GetValue(contactToMod, null).ToString());
}
Debug.Log ("Kappa");
But it only display:
contactToMod.Firstname = John Snow
--> Nothing <--
Kappa
And my previous code was working well so the object contains well defined values
And when you look in the inspector contactTo$$anonymous$$od.Firstname has a value in it e.g. Sam?
Ye, it have a value in it. But the class which is sending datas to PHP (and looping through properties) is a static class so it don't inherit from $$anonymous$$onobehaviour, I don't know if it's important to specify it.
You would think. Try making the class no-static (I realize there will be some code changes).
Answer by sam1902 · May 04, 2016 at 02:28 PM
Ok, finally since I don't get any answer (I know I'm not patient but time is running out for me..) I'll instead of trying to get each property's name/value use XML Serializer to serialize/unserialize my class:
public string ToXML()
{
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(this.GetType());
serializer.Serialize(stringwriter, this);
return stringwriter.ToString();
}
public static YourClass LoadFromXMLString(string xmlText)
{
var stringReader = new System.IO.StringReader(xmlText);
var serializer = new XmlSerializer(typeof(YourClass ));
return serializer.Deserialize(stringReader) as YourClass ;
}
Ho and if someone want to use it, I found it here and don't forget to add those [XMLStrangeTag] above your class's properties (c.f. wiki ).
Your answer
Follow this Question
Related Questions
How to use hold/tap interactions in new Input System 0 Answers
How to alter the bounce of a ball? Add force when hitting a ball? 0 Answers
Magnetic pieces being attached to each other and moving together ??? 0 Answers
Why is the 'parent' property on transform and not gameObject? 3 Answers
How to reference assets that are inside a folder?,Referencing assets in the scene 0 Answers