Ошибка сериализацией листа обьектов - C#

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
  2. Additional information: There was an error reflecting type 'System.Collections.Generic.List`1[cursova6.Client]'.
Вот ошибка. а вот тут тот класс который хочу сериализовать.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. namespace cursova6
  8. {
  9. [Serializable]
  10. public class Client
  11. {
  12. public string Name{get;set;}
  13. public string Surname { get; set; }
  14. public int Npassport { get; set; }
  15. public string Adres { get; set; }
  16. public int Telefon { get; set; }
  17. public int Nombank { get; set; }
  18. public string Tipneruh { get; set; }
  19. public int Vcena { get; set; }
  20. public List<Realty> Neruh { get; set; }
  21. public Client(string name, string surname, int npassport, string adres, int telefon, int nombank, string tipneruh, int vcena)
  22. {
  23. Name = name;
  24. Surname = surname;
  25. Npassport = npassport;
  26. Adres = adres;
  27. Telefon = telefon;
  28. Nombank = nombank;
  29. Tipneruh = tipneruh;
  30. Vcena = vcena;
  31. Neruh = new List<Realty>();
  32. }
  33. public Client()
  34. {
  35. }
  36. public string Description()
  37. {
  38. var sb = new StringBuilder();
  39. PropertyInfo[] properties = this.GetType().GetProperties();
  40. foreach (PropertyInfo propertyInfo in properties)
  41. {
  42. sb.Append(propertyInfo.GetValue(this));
  43. sb.Append(" ");
  44. }
  45. return sb.ToString();
  46. }
  47. }
  48. }
и содержимое обьекта что есть в етом классе
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. namespace cursova6
  8. {
  9. public class Realty
  10. {
  11. public string Name { get; set; }
  12. public double Vartist { get; set; }
  13. public string Mrozt { get; set; }
  14. public Firstvlas Popvlas{get; set;}
  15. public Realty(string name, double vartist, string mrozt,ref Firstvlas popvlas)
  16. {
  17. Name = name;
  18. Vartist = vartist;
  19. Mrozt = mrozt;
  20. Popvlas = popvlas;
  21. }
  22. public string Description()
  23. {
  24. var sb = new StringBuilder();
  25. PropertyInfo[] properties = this.GetType().GetProperties();
  26. foreach (PropertyInfo propertyInfo in properties)
  27. {
  28. sb.Append(propertyInfo.GetValue(this));
  29. sb.Append(" ");
  30. }
  31. return sb.ToString();
  32. }
  33. }
  34. }
Ну и тд) Мне надо что бы полностю всю инфу сериализовало)) Ну + вот такая ошибка непонятная вылетает Вот как я сериализую
Листинг программы
  1. public static void serilaze<T>(T vector, string filename)
  2. {
  3. XmlSerializer writer = new XmlSerializer(vector.GetType());
  4. var path = filename;
  5. FileStream file = new FileStream(path, FileMode.Truncate);
  6. writer.Serialize(file, vector);
  7. file.Close();
  8. }
  9. public static T desirelize<T>(string filename)
  10. {
  11. System.Xml.Serialization.XmlSerializer reader =
  12. new System.Xml.Serialization.XmlSerializer(typeof(T));
  13. System.IO.StreamReader file = new System.IO.StreamReader(
  14. filename);
  15. T overview;
  16. overview = (T)reader.Deserialize(file);
  17. file.Close();
  18. return overview;
  19. }
и десериализация на всякий)

Подскажет кто в чем причина ошибки? и как ее решить?

Вот в етой строчке выбивает ексепшен рание описан
Листинг программы
  1. XmlSerializer writer = new XmlSerializer(vector.GetType());
Проблема решена добавлением дефаулт конструкторов в те вложеные обьекты. Сонливость дает знать о себе. Пора походу спааать

Решение задачи: «Ошибка сериализацией листа обьектов»

textual
Листинг программы
  1. var formatter = new XmlSerializer(typeof(List<Client>), new [] { typeof(Realty)});

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 3.714 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы