Ошибка сериализацией листа обьектов - C#
Формулировка задачи:
Листинг программы
- An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
- Additional information: There was an error reflecting type 'System.Collections.Generic.List`1[cursova6.Client]'.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- namespace cursova6
- {
- [Serializable]
- public class Client
- {
- public string Name{get;set;}
- public string Surname { get; set; }
- public int Npassport { get; set; }
- public string Adres { get; set; }
- public int Telefon { get; set; }
- public int Nombank { get; set; }
- public string Tipneruh { get; set; }
- public int Vcena { get; set; }
- public List<Realty> Neruh { get; set; }
- public Client(string name, string surname, int npassport, string adres, int telefon, int nombank, string tipneruh, int vcena)
- {
- Name = name;
- Surname = surname;
- Npassport = npassport;
- Adres = adres;
- Telefon = telefon;
- Nombank = nombank;
- Tipneruh = tipneruh;
- Vcena = vcena;
- Neruh = new List<Realty>();
- }
- public Client()
- {
- }
- public string Description()
- {
- var sb = new StringBuilder();
- PropertyInfo[] properties = this.GetType().GetProperties();
- foreach (PropertyInfo propertyInfo in properties)
- {
- sb.Append(propertyInfo.GetValue(this));
- sb.Append(" ");
- }
- return sb.ToString();
- }
- }
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- namespace cursova6
- {
- public class Realty
- {
- public string Name { get; set; }
- public double Vartist { get; set; }
- public string Mrozt { get; set; }
- public Firstvlas Popvlas{get; set;}
- public Realty(string name, double vartist, string mrozt,ref Firstvlas popvlas)
- {
- Name = name;
- Vartist = vartist;
- Mrozt = mrozt;
- Popvlas = popvlas;
- }
- public string Description()
- {
- var sb = new StringBuilder();
- PropertyInfo[] properties = this.GetType().GetProperties();
- foreach (PropertyInfo propertyInfo in properties)
- {
- sb.Append(propertyInfo.GetValue(this));
- sb.Append(" ");
- }
- return sb.ToString();
- }
- }
- }
Листинг программы
- public static void serilaze<T>(T vector, string filename)
- {
- XmlSerializer writer = new XmlSerializer(vector.GetType());
- var path = filename;
- FileStream file = new FileStream(path, FileMode.Truncate);
- writer.Serialize(file, vector);
- file.Close();
- }
- public static T desirelize<T>(string filename)
- {
- System.Xml.Serialization.XmlSerializer reader =
- new System.Xml.Serialization.XmlSerializer(typeof(T));
- System.IO.StreamReader file = new System.IO.StreamReader(
- filename);
- T overview;
- overview = (T)reader.Deserialize(file);
- file.Close();
- return overview;
- }
Подскажет кто в чем причина ошибки? и как ее решить?
Вот в етой строчке выбивает ексепшен рание описан
Листинг программы
- XmlSerializer writer = new XmlSerializer(vector.GetType());
Проблема решена добавлением дефаулт конструкторов в те вложеные обьекты. Сонливость дает знать о себе. Пора походу спааать
Решение задачи: «Ошибка сериализацией листа обьектов»
textual
Листинг программы
- var formatter = new XmlSerializer(typeof(List<Client>), new [] { typeof(Realty)});
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д