0

I have the following deserialization function.

private object Deserialize(string file)
{
   var ret = new object(); 
   var fmt = new BinaryFormatter(); 
   using(FileStream fs = File.Open(file, FileMode.Open)) 
   {
      ret = fmt.Deserialize(fs); 
   } 

   return ret; 
} 

I call the function and cast it as a generic list of the expected type.

var list = Deserialize(file) as List<Something>;

But I get a null value. Debugging the code shows that the function works. The result object is a list.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
PointedC
  • 101
  • 8

3 Answers3

0

For one, File.Open does not have an overload that takes a single argument. At the very minimum, you need to specify the FileMode as well.

FileStream fs = File.Open(file,FileMode.Open)

Secondly, Filestream does not have a member method Deserialize, so fs.Deserialize is invalid.

To help you further, what kind of a file are you attempting to deserialize. CSV, XML...

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • Thanks, I deserialize a binary file,and it should be fmt not fs **private Object Deserialize(string file) { Object ret=new Object(); var fmt=new BinaryFormatter(); using(FileStream fs=File.Open(file, FileMode.Open)) { ret=fmt.Deserialize(fs); } return ret; }** – PointedC Nov 16 '11 at 04:03
  • @PointedC, please edit your question and put the compilable code there, not here in the comments. – Joe White Nov 16 '11 at 04:15
0
List<Something> ret;
ret=(List<Something>)serializer.fmt(fs); 
Brandon Moore
  • 8,590
  • 15
  • 65
  • 120
  • Oh thanks I use the above function in my OP because I have other lists (List...). Debug shows that the above function works, I get the ret value as expected but right after the line of the function call, the value return is null. – PointedC Nov 16 '11 at 04:17
  • Well aside from the things xbonez pointed out, I don't believe there is anything we'd be able to tell from this. If you're going to use this to deserialize various types then I would recommend using a generic method: http://msdn.microsoft.com/en-us/library/twcad0zb%28v=vs.80%29.aspx – Brandon Moore Nov 16 '11 at 04:33
  • Maybe the problem is in your serialization? – Brandon Moore Nov 16 '11 at 04:34
0

I wrote a program that serializes/deserializes some data based on the code you provided, and it works fine.

You said:

But I get a null value. Debugging the code shows that the function works. The result object is a list.

There is a subtle difference between 2 casting operations in C#

string text = (string)GetString(); // throws exception if cannot be cast
string text = GetString() as string; // returns null if source cannot be cast

Other than that, based on the limited amount of info that you provided, we won't be able to help you further troubleshoot.

Can you narrow it down to the line of code that isn't working? Are you sure that the serialization is working properly?

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Cereal
{
  class Program
  {
    static void Main(string[] args)
    {
      string file = Path.Combine(Environment.CurrentDirectory, "cereal.bin");

      List<string> namesToWrite = new List<string>();
      namesToWrite.Add("Frosted Flakes");
      namesToWrite.Add("Fruit Loops");
      namesToWrite.Add("Lucky Charms");
      namesToWrite.Add("Apple Jacks");

      Serialize(file, namesToWrite);

      List<string> namesToRead = Deserialize(file) as List<string>;;

      foreach (string name in namesToRead)
      {
        Console.WriteLine(name);
      }

      Console.ReadLine();
    }

    static void Serialize(string file, object stuff)
    {
      var fmt = new BinaryFormatter();

      using (FileStream fs = File.Open(file, FileMode.Create))
      {
        fmt.Serialize(fs, stuff);
      }
    }

    static object Deserialize(string file)
    {
      var ret = new object();
      var fmt = new BinaryFormatter();

      using (FileStream fs = File.Open(file, FileMode.Open))
      {
        ret = fmt.Deserialize(fs);
      }

      return ret;
    }
  }
}
Community
  • 1
  • 1
JohnB
  • 18,046
  • 16
  • 98
  • 110