问题描述
我有以下的code:
class Program
{
static void Main(string[] args)
{
string xml = @"<ArrayOfUserSetting>
<UserSetting>
<Value>Proposals</Value>
<Name>LastGroup</Name>
</UserSetting>
<UserSetting>
<Value>Visible</Value>
<Name>WidgetsVisibility</Name>
</UserSetting>
</ArrayOfUserSetting>";
List<UserSetting> settings =
GetObjFromXmlDocument<List<UserSetting>>(xml);
}
public static T GetObjFromXmlDocument<T>(string xml)
{
T customType;
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
using (XmlNodeReader xmlNodeReader = new XmlNodeReader(xmlDocument))
{
customType = (T)serializer.Deserialize(xmlNodeReader);
}
return customType;
}
}
[Serializable]
public class UserSetting
{
public string Value { get; set; }
public string Name { get; set; }
}
在code正常工作,并调用GetObjFromXmlDocument产生一个List集合。但是,我总是第一个机会异常mscorlib.dll中,键入 System.IO.FileNotFoundException
时, XmlSerializer的序列化=新的XmlSerializer(typeof运算(T ));
执行
于是我走进调试/异常,并开启托管调试助手。我得到了以下在该行:
So I went into Debug/Exception and turned on Managed Debugging Assistants. I got the following on that line:
与显示名称mscorlib.XmlSerializers的组件未能加载在'LoadFrom'结合的AppDomain的上下文ID 1.失败的原因是信息:System.IO.FileNotFoundException:无法加载文件或程序集 mscorlib.XmlSerializers,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089或它的某一个依赖。该系统找不到指定的文件。 文件名:mscorlib.XmlSerializers,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089
有人可以解释为什么发生这种情况?有什么我可以做的 UserSetting
类来使问题消失?该应用程序是相当敏感的表现,我宁愿没有例外。
Can someone explain why this is happening? Is there something I could do to the UserSetting
class to make the problem disappear? The application is quite performance sensitive and I'd rather not have the exception.
推荐答案
微软称:
XmlSerializer的尝试加载 pre生成的序列化,以避免 的序列化code编译 上飞。有没有简单的方法来 检查将装配由被发现 所述的Assembly.Load()调用,这将是 复制融合路径搜索和 在XmlSerializer的加载逻辑。
它看起来像一个FileNotFound异常被抛出,并在XmlSerializer的处理时,将产生pre-生成序列不能被发现,这将进而导致序列化code。
It looks like a FileNotFound exception is thrown and handled in the XmlSerializer when the "pre-generated serializer" cannot be found, which will then cause the serialization code to be generated.
这篇关于为什么我会得到一个序列化错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!