Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sam1902 · May 04, 2016 at 01:36 PM · programming

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TBruce · May 04, 2016 at 01:44 PM 0
Share

@sam1902

And when you look in the inspector contactTo$$anonymous$$od.Firstname has a value in it e.g. Sam?

avatar image sam1902 TBruce · May 04, 2016 at 01:49 PM 0
Share

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.

avatar image TBruce sam1902 · May 04, 2016 at 01:51 PM 0
Share

@sam1902

You would think. Try making the class no-static (I realize there will be some code changes).

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 ).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges