Using reflection to load values from XML into a structure

I’m using Reflection to load some values from an XML into a structure. All works but I got a problem to manage the array. With an Int32 it’s ok, but how can I manage an Int32[]?

EDIT: Here’s my code … it’s not complete due the lenght … I want to improve the save and load for the array types.

    //My struct ... it should be a class too          public struct stOptions     {         public int IntNumber;         public double DoubleNumber;         public string String;         public Point aPoint;     }      //How my class works          private void Form1_Load(object sender, EventArgs e)     {         stOptions options = new stOptions();         //Populate the struct           options.aPoint = new Point(12, 24);         options.DoubleNumber = 34d;         options.IntNumber = 17;         options.String = "Hello";          ManageSettings ms = new ManageSettings();          ms.SaveSettings("e:\\test001.xml", options);          options = default(stOptions);          options = ms.LoadSettings<stOptions>("e:\\test001.xml");     }      //A portion of my class      public T LoadSettings<T>(string FileName)     {         Type type = typeof(T);         var returnObject = Activator.CreateInstance(type);         List<Settings> list = null;          try         {             using (StreamReader reader = File.OpenText(FileName))             {                 XmlSerializer serializer = new XmlSerializer(typeof(List<Settings>));                 list = (List<Settings>)serializer.Deserialize(reader);             }         }         catch         {             //Error accessing the file             _errors.Add("Unable to locate the file or access denied: " + FileName);             return default(T);         }          try         {             foreach (Settings entry in list)             {                 FieldInfo field = returnObject.GetType().GetField(entry.Key.ToString());                 if (field != null)                 {                     SetField(field, entry.Value.ToString(), returnObject);                 }                 field = null;                  PropertyInfo prop = returnObject.GetType().GetProperty(entry.Key.ToString());                 if (prop != null)                 {                     SetField(prop, entry.Value.ToString(), returnObject);                 }                 prop = null;             }             list = null;         }         catch         {             //Error processing the XML             _errors.Add("Errore processing the XML file: " + FileName);             return default(T);         }         return (T)returnObject;     }          private void SetField(FieldInfo prop, string value, object returnObject)     {         switch (prop.FieldType.Name.ToLower())         {             case "uint16":                 prop.SetValue(returnObject, Convert.ToUInt16(value));                 break;              case "uint32":                 prop.SetValue(returnObject, Convert.ToUInt32(value));                 break;                              etc.             [...]                          default:                 //An unmanaged type                 Debug.WriteLine("Found an unmanaged type: " + prop.FieldType.Name);                 break;         }     } 

When it is completed I will publish it.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.