Рассмотрим следующее:
public static class FileSerializer
{
public static void SaveToFile<T>(this T obj, String fileName)
{
String dbFile = Path.Combine(Application.StartupPath, fileName);
using (Stream stream = File.Open(dbFile, FileMode.Create))
{
BinaryFormatter bFormatter = new BinaryFormatter();
lock (obj) bFormatter.Serialize(stream, obj);
}
}
public static void LoadFromFile<T>(this T obj, String fileName, Boolean ensureExists)
{
String dbFile = Path.Combine(Application.StartupPath, fileName);
if (!File.Exists(dbFile))
if (ensureExists)
throw new FileNotFoundException("File not Found!");
else return;
using (Stream stream = File.Open(dbFile, FileMode.Open))
{
if (stream.Length > 0)
{
BinaryFormatter bFormatter = new BinaryFormatter();
obj = (T)bFormatter.Deserialize(stream);
}
}
}
}
Несмотря на то, что я присоединяю отладчик, и объект obj в последней строке кода выше имеет записи, когда я использую метод как таковой:
lstServers.LoadFromFile("Servers.dat", false);
lstServers равно ВСЕГДА пусто.
Есть идеи, почему?