public void Load<T>(List<T> list, string Path)
{
try
{
using (Stream stream = File.Open(Path, FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
List<T> data = (List<T>)bin.Deserialize(stream);
list = data;
}
}
catch
{
list = new List<T>();
}
}
public UserManager(string path)
{
Load<User>(UserList, path);
}
这种做法list总是null,改成下面这样就可以,为什么?..
public List<T> Load<T>(string Path)
{
try
{
using (Stream stream = File.Open(Path, FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
List<T> data = (List<T>)bin.Deserialize(stream);
return data;
}
}
catch
{
return null;
}
}
public UserManager(string path)
{
if (Load<User>(path) != null)
UserList = Load<User>(path);
else
UserList = new List<User>();
}
https://docs.microsoft.com/zh...