Deserialization throws InvalidCastException if POCO has added additional property after serialization.
nsatyanarayana-conga opened this issue · 0 comments
nsatyanarayana-conga commented
I am using Hyperion 0.9.15 and testing below scenario.
Created POCO object and did serialization and stored stream in file stream. Then I have added another new property and try to
de-serialize it where I am getting InvalidCastException. I tried with versiontolerance enabled and disabled having same issue.
Is this problem may be related to Hyperion or something i'm missing?. Guide me how to solve my issue if I am missing any configuration.
----------------------------POCO--------------------
public class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
}
------------------------Serialize to File---------------------------------
public void FileSerialize()
{
var employee = new Employee
{
Name = "Foo",
Salary = 2500.0
};
var fileName = @"C:\FromDDrive\words.txt";
using FileStream fs = File.OpenWrite(fileName);
_serializer.Serialize(employee, fs);
Assert.NotNull(fs);
}
-----------------------------Updated POCO----------------
public class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
public int Age { get; set; }
}
-----------------De-serialize------------------------------------
public void FileSerializeRead()
{
var fileName = @"C:\FromDDrive\words.txt";
using FileStream fs = File.OpenRead(fileName);
//_serializer.Serialize(employee, fs);
//fs.Position = 0;
var actual = _serializer.Deserialize<Employee>(fs);
Assert.Equal(employee.Name, actual.Name);
Assert.NotNull(fs);
}