Я сделал простой маленький проект, чтобы воспроизвести проблему в более крупном.У меня есть 3 класса, унаследованных от ModelBase
public class Contact : ModelBase
{
[IncludeInSerialization]
public string FirstName { get; set; }
[IncludeInSerialization]
public string LastName { get; set; }
[IncludeInSerialization]
public DateTime DateOfBirth { get; set; }
[IncludeInSerialization]
public string PhoneNumber { get; set; }
[IncludeInSerialization]
public string Email { get; set; }
[IncludeInSerialization]
public ICollection<Address> Addresses { get; set; }
}
public class Lookup : ModelBase
{
[IncludeInSerialization]
public string Value { get; set; }
[IncludeInSerialization]
public string Group { get; set; }
}
public class Address : ModelBase
{
[IncludeInSerialization]
public Lookup Country { get; set; }
[IncludeInSerialization]
public Lookup StateOrTerritory { get; set; }
[IncludeInSerialization]
public string AddressText { get; set; }
public override string ToString()
{
return $"{AddressText}\r\n{StateOrTerritory?.Value},{Country?.Value}";
}
}
, и основная программа
class Program
{
static void Main(string[] args)
{
Contact customer = new Contact();
customer.FirstName = "Sam";
customer.LastName = "Smith";
customer.DateOfBirth = DateTime.Now.AddYears(-30);
customer.Addresses = new HashSet<Address>();
Lookup australia = new Lookup {Group = "Countries", Value = "Australia"};
Lookup NSW = new Lookup {Group = "StatesAndTerritories", Value = "NSW"};
Address firstAddress = new Address();
firstAddress.Country = australia;
firstAddress.StateOrTerritory = NSW;
firstAddress.AddressText = "1/1 A street, Sydney";
Address secondAddress = new Address();
secondAddress.Country = australia;
secondAddress.StateOrTerritory = NSW;
secondAddress.AddressText = "2/2 Another street, Sydney";
customer.Addresses.Add(firstAddress);
customer.Addresses.Add(secondAddress);
((IEditableObject)customer).BeginEdit();
Console.WriteLine("Addresses Before:");
Console.WriteLine();
foreach (Address address in customer.Addresses)
{
Console.WriteLine(address.ToString());
Console.WriteLine();
}
((IEditableObject)customer).CancelEdit();
Console.WriteLine("--------------------");
Console.WriteLine();
Console.WriteLine("Addresses After:");
Console.WriteLine();
foreach (Address address in customer.Addresses)
{
Console.WriteLine(address.ToString());
Console.WriteLine();
}
Console.Read();
}
}
, выполняющая программу, дает различные результаты для Addresses
до и после десериализации.
Обратите внимание на вызов BeginEdit
, который запускает сериализацию перед первой печатью, и вызов CancelEdit
, который запускает десериализацию перед печатью во второй раз.
Кто-нибудь знает, что происходит?
Файлы: https://github.com/3m3sd1/Testing_Catel_Serialization/tree/master