2

The problem is that i get zero count in repo when deserialize it (without any errors)

[Serializable]
class RegexRepository : Dictionary<string, string>
{
    public RegexRepository()
    {
        //this.Add("All", "ALL");
        //this.Add("Name", @"Name:(?<data>[\s\w]+)Email");
        //this.Add("Email", @"Email:(?<data>[\w\s@]+\.com)");
        //this.Add("Phone Number", "Phone:(?<data>\\d+)");
    }
    protected RegexRepository(SerializationInfo info, StreamingContext context)
    {

    }
    private static RegexRepository repo = new RegexRepository();
    public static RegexRepository Instance
    {
        get
        {
            if (repo == null)
            {
                repo = new RegexRepository();
            }
            return repo;
        }
    }
    string FileName = AppDomain.CurrentDomain.BaseDirectory + "BinaryFile.dat";
    public void Serialize()
    {
        using (FileStream ms = new FileStream(FileName, FileMode.OpenOrCreate))
        {
            var bf = new BinaryFormatter();
            bf.Serialize(ms, this);
        }
    }
    public void Deserialize()
    {
        if (System.IO.File.Exists(FileName))
        {
            using (FileStream ms = new FileStream(FileName, FileMode.Open))
            {
                var bf = new BinaryFormatter();
                repo = (RegexRepository)bf.Deserialize(ms);
               //Here i get zero count in repo, checked while debugging
            }
        }
    }
}

I have seen BinaryFile.dat is not empty and i can see some records in it. Kindly help me

Charlie
  • 4,827
  • 2
  • 31
  • 55
  • I guess you are having this issue because the Dictionary class that you have derived from is not marked as serializable. In this case I think you need a surrogate. This may be your [answer](http://stackoverflow.com/questions/13166105/is-it-possible-to-do-net-binary-serialization-of-an-object-when-you-dont-have). The question is do you really need to derive your class from the Dictionary class? – Kosala W Nov 10 '15 at 06:24
  • @KosalaW - `Dictionary` is serializable. See the [reference source](http://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs). – dbc Nov 10 '15 at 06:29
  • @KosalaW it will be easy for me if i don't use derived class? kindly check answer of dbc. he says that i don't need filename. How? – Charlie Nov 10 '15 at 06:30
  • @Charlie - What I meant is that it doesn't need to be an instance field. See updated answer. – dbc Nov 10 '15 at 06:37
  • @Charlie: Yes. Just checked. dbcc is correct. Dictionary is [serializable](https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx). So you need to call the base constructor. – Kosala W Nov 10 '15 at 06:41

2 Answers2

2

You need to call the base constructor from your streaming constructor:

    protected RegexRepository(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

Also, FileName probably should not be a field, you're allocating memory for it in your class, which is not necessary. Instead a static property would seem to make more sense:

    static string FileName
    {
        get
        {
            return AppDomain.CurrentDomain.BaseDirectory + "BinaryFile.dat";
        }
    }
dbc
  • 104,963
  • 20
  • 228
  • 340
2

When the base class implements ISerializable, it isn't enough to just slap a [Serializable] attribute on a derived class. Your derived class needs to:

  1. Call base(info, context) from the (currently empty) serialization constructor.
  2. Fill in your serialization constructor to read any instance values from the SerializationInfo.
  3. Create a GetObjectData(SerializationInfo info, StreamingContext context) override to write derived members to SerializationInfo (and call the base class' implementation).

Running VS code analysis on your class will light up the problems.

Mark Waterman
  • 961
  • 7
  • 16