简体   繁体   中英

Deserializing a null value in an object C# WPF

I am serializing an object of employee class. Some of the properties in the class might be null. I need to deserialize the object with the null values, so that the null properties remail null. I am getting TargetInvocationException when I try to deserialize null values. Please Help me out

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;
   }

}

I tried your code in LinqPad (with a few mods, the code above would never compile or work). This works fine:

void Main()
{
    Employee emp = new Employee();
    emp.Id = "1";

    const string path = @"C:\Documents and Settings\LeeP\Sample.erl";

    emp.Dump();

    using(var stream = File.Open(path, FileMode.Create))
    {
        var formatter = new BinaryFormatter();
        formatter.Serialize(stream, emp);
    }

    using(var stream = File.Open(path, FileMode.Open))
    {
        var formatter = new BinaryFormatter();
        var sample = (Employee)formatter.Deserialize(stream); 

        sample.Dump();
    }
}

// You need to mark this class as [Serializable]
[Serializable]
public class Employee
{
    public string Name {get; set;}
    public string Id{get;set;}
}

You only need to apply the required attribute as mentioned above.

namespace SO
{
  using System;
  using System.IO;
  using System.Runtime.Serialization.Formatters.Binary;

  [Serializable]
  public class Employee
  {
    public string Name { get; set; }

    public string Id { get; set; }
  }

  public class Test
  {
    public static void Main()
    {
      var emp = new Employee { Id = "1" };
      //Name field is intentionally uninitialized

      using (var stream = File.Open("Sample.erl", FileMode.Create))
      {
        var bformatter = new BinaryFormatter();

        bformatter.Serialize(stream, emp);
      }

      using (var stream = File.Open("Sample.erl", FileMode.Open))
      {
        var bformatter = new BinaryFormatter();

        var empFromFile = (Employee)bformatter.Deserialize(stream);

        Console.WriteLine(empFromFile.Id);
        Console.ReadLine();
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM