Witam,
mam problem z deserializacją. Wszystko fajnie działa tylko chciałbym zrobić tak, żeby miał dostęp do wszystkich pól z deserializowanego obietku.
Oto mój kod:
//deserializowanie obiektu
public ICollection<T> Deserialization<T>()
{
var fs = new FileStream(this.FileName, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
List<T> list = new List<T>();
while (fs.Position != fs.Length)
{
var deserialized = (T)bf.Deserialize(fs);
list.Add(deserialized);
}
fs.Close();
return list;
}
//Wyświetlenie deserializowanego obietku,
//tylko chciałbym, żebym miał dostęp do pól pzw oraz specjalizacja z modelu doktor.
//A jak dam tym Employee w serializacji to tylko mam dostęp do pól z modelu Employee
//a jak wpiszę typ Doktor to wyskakuje mi wyjątek, że nie mogę się odwołać do obietku.
var result = create.Deserialization<Employee>();
Console.WriteLine("List pracowników");
foreach (var item in result)
{
Console.WriteLine($"#Nazwa użytkownika: {item.Username} Imię: {item.FirstName} -- Naziwsko: {item.LastName} -- Posada: {item.Type} -- Specjalizacja: {item.Specialization}");
}
//Model pracownik
[Serializable]
public class Employee : IEmployees
{
public enum TypeOfEmployee
{
admin,
doktor,
pielęgniarka
}
public string Username { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public long Pesel { get; set; }
public TypeOfEmployee? Type { get; set; }
}
//Model doktor
[Serializable]
public class Doktor : Employee
{
public enum SpecializationEnum
{
laryngolog,
kardiolog,
urolog,
neurolog
}
public string Pzw { get; set; }
public SpecializationEnum? Specialization { get; set; }
}
//Interfejs do pracowników
public interface IEmployees
{
string Username { get; set; }
string Password { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
long Pesel { get; set; }
TypeOfEmployee? Type { get; set; }
}