Guy*_*ito 5 .net c# serialization
在.net C中第一次使用二进制格式#
MSDN的代码是这样的:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.lvl", FileMode.Create, FileAccess.Write,FileShare.None);
formatter.Serialize(stream, Globals.CurrentLevel);
stream.Close();
Run Code Online (Sandbox Code Playgroud)
只是想知道我应该IFormatter在我的班级中存储一个字段并一遍又一遍地使用它,还是我应该如上所述并在每次保存/加载某些东西时实例化一个新的?
我注意到它不是IDisposable.
重新创建a的开销很小BinaryFormatter,它在构造函数中设置的大多数属性都是enums,请参见此处(感谢Reflector):
public BinaryFormatter()
{
this.m_typeFormat = FormatterTypeStyle.TypesAlways;
this.m_securityLevel = TypeFilterLevel.Full;
this.m_surrogates = null;
this.m_context = new StreamingContext(StreamingContextStates.All);
}
Run Code Online (Sandbox Code Playgroud)
如果你打算重新使用它,你需要同步访问Serialize和Deserialize方法来保持它们的线程安全.
| 归档时间: |
|
| 查看次数: |
874 次 |
| 最近记录: |