只为别人谁运行到这个问题;与上面的答案,从MSDN的例子武装我设法使用下面的类来解决这个问题:
public static class XmlSerializerCache
{
private static readonly Dictionary<string, XmlSerializer> cache =
new Dictionary<string, XmlSerializer>();
public static XmlSerializer Create(Type type, XmlRootAttribute root)
{
var key = String.Format(
CultureInfo.InvariantCulture,
"{0}:{1}",
type,
root.ElementName);
if (!cache.ContainsKey(key))
{
cache.Add(key, new XmlSerializer(type, root));
}
return cache[key];
}
}
然后,而不是使用默认的XmlSerializer的构造函数需要一个XmlRootAttribute,我用,而不是执行以下操作:
var xmlRootAttribute = new XmlRootAttribute("ExampleElement");
var serializer = XmlSerializerCache.Create(target.GetType(), xmlRootAttribute);
我的应用程序现在正在执行!
好的,看起来像问题是如果您为序列化程序指定了任何非类型参数,则会为每个序列化程序实例生成序列化程序集!这就是为什么 - 我认为 - 我看到如此糟糕的表现。 有谁知道为什么默认的XmlSerializer会这样做的任何原因?我不明白为什么只需指定根节点名称就意味着缓存无法使用? – Dougc 2009-10-09 17:32:36