我正在序列化员工类的对象。类中的某些属性可能为空。我需要使用空值反序列化对象,以便空属性重新发送空值。当我尝试反序列化空值时,我收到了 TargetInvocationException。请帮帮我
public class Employee
{
public string Name {get; set;}
public string Id{get;set;}
}
public mainclass
{
public void MainMethod()
{
Employee emp = new Employee();
emp.ID = 1;
//Name field is intentionally uninitialized
Stream stream = File.Open("Sample.erl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, sample);
stream.Close()
stream = File.Open("Sample.erl", FileMode.Open);
bformatter = new BinaryFormatter();
sample = (Employee)bformatter.Deserialize(stream); //TargetInvocationException here
//It works fine if Name field is initialized
stream.Close();
Textbox1.text = sample.Name;
Textbox2.text = sample.ID;
}
}