0

Have a "User class" in my C# project for wich i would like to store all variables in an text file - All goes fine until i come to a list of "to do" list that is included in the user class. The User class also includes a couple of more lists

So the question is, how do i cycle through all variables in an object, and all variables in the included lists?

The code i got so far:

                    StreamWriter File = new StreamWriter(Sökväg);

                    var props = this.GetType().GetProperties();
                    foreach (var prop in props)
                    {
                        object value = prop.GetValue(this, null); // against prop.Name
                        if (value != null)
                        {
                            switch (prop.PropertyType.ToString())
                            {
                                case "System.String":
                                case "System.Int16":
                                case "System.Int32":
                                case "System.Int64":
                                case "System.DateTime":
                                case "System.Boolean":
                                    File.WriteLine(prop.Name + "|" + value.ToString());
                                    break;
                                default:
                                    MessageBox.Show("Property type not supported" +
                                    Environment.NewLine + prop.Name);
                                    break;
                            }
                        }
                    }
                    File.Close();
  • 2
    Why not use a standard serialization format like JSON that has solved these types of problems already? Or is this a learning experience? – D Stanley Sep 27 '16 at 19:58
  • Have you stepped through the loop in the debugger to see what information you get back from `prop.GetValue` when the property is a list? – ChrisF Sep 27 '16 at 19:58
  • I think you need to look into [XML serialization](http://stackoverflow.com/questions/4123590/serialize-an-object-to-xml). What you want to do is convert a tree of object/properties into some kind of text representation. For that, you want a hierarchical text format like XML or JSON. If you're having this much trouble writing to the file, reading it is going to be an ugly experience. – 15ee8f99-57ff-4f92-890c-b56153 Sep 27 '16 at 19:58
  • I would personally use `switch (Type.GetTypeCode(prop.PropertyType)) ...` – Mr Anderson Sep 27 '16 at 20:07
  • Thanks for your tips, i will look into JSON and XML serialization - may be what i'm going for... – M.Johansson Sep 28 '16 at 08:39

1 Answers1

0

Reflection is a pretty expensive thing to do in a production environment. Having said that, you first want to refactor all your code between the first and last line into a new method. Then you can add additional case statements for lists and iterate through the elements and recursively call the original method that was refactored.

Barka
  • 8,764
  • 15
  • 64
  • 91