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