BinaryFormatter序列化和反序列化线程安全吗? [英] Are BinaryFormatter Serialize and Deserialize thread safe?
本文介绍了BinaryFormatter序列化和反序列化线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
引用这可以改写为:
private static BinaryFormatter formatter = new BinaryFormatter();
public static T DeepClone<T>(this T a)
{
using(MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, a);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
那么要避免为每个调用构造(和GC'ing)新的BinaryFormatter?
So avoiding constructing (and GC'ing) a new BinaryFormatter for each call?
此代码路径非常频繁,因为它涉及我们的缓存层,我希望使其尽可能轻巧.
This code path is getting hit very frequently as it involves our caching layer and I would like to make it as lightweight as possible.
谢谢.
推荐答案
查看全文