I have the following method to serialize a list of classes to disk:
private static void Serialize(List<CcmAlarmUalUsl> entityList)
{
try
{
using (Stream stream = File.Open("CcmAlarmUalUsl.dat", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, entityList);
}
}
catch (IOException)
{
}
}
The entity class I want to serialize looks like this:
public class CcmAlarmUalUsl
{
public string SubId { get; set; }
public System.DateTime TimeOfAlarm { get; set; }
public int AlarmType { get; set; }
}
Is my Serialize class the best way to serialize the list of entities to disk? This does not need to be human readable. It is to be used strictly by the application, as a storage means for an internal cache. No database storage is necessary.
I’m also a bit concerned about when it comes time to modify the structure of CcmAlarmUalUsl. I could be adding properties to the end of the class. How will this affect existing cached items?
Simple answer – no. Your code won’t compile (
lizards1??) and yourCcmAlarmUalUs1class isn’t marked as serializable (run-time error). Mark yourCcmAlarmUrlUs1class as serializable, changelizards1toentityListand it should work and it looks like you’re doing things fine.You’re question is kind of vague, though. By ‘best’, what are you looking for? Without knowing that, it’s hard to really give a good answer.