我正在尝试将保存方法添加到可以调用并将对象序列化到文件的列表中。除了如何获取基类本身之外,我已经弄清楚了一切。
这是我的代码:
/// <summary>
/// Inherits the List class and adds a save method that writes the list to a stream.
/// </summary>
/// <typeparam name="T"></typeparam>
class fileList<T> : List<T>
{
private static IFormatter serial = new BinaryFormatter();
private Stream dataStream;
/// <summary>
/// path of the data file.
/// </summary>
public string dataFile { get; set; }
/// <summary>
/// Sets the datafile path
/// </summary>
public fileList(string dataFile)
{
this.dataFile = dataFile;
}
/// <summary>
/// Saves the list to the filestream.
/// </summary>
public void Save()
{
dataStream = new FileStream(dataFile,
FileMode.Truncate, FileAccess.Write,
FileShare.Read);
//Right here is my problem. How do I access the base class instance.
serial.Serialize(dataStream, this.base);
dataStream.Flush();
dataStream.Close();
dataStream = null;
}
}