为什么我会收到序列化错误?

我有以下代码:

class Program { static void Main(string[] args) { string xml = @"  Proposals LastGroup   Visible WidgetsVisibility  "; List settings = GetObjFromXmlDocument<List>(xml); } public static T GetObjFromXmlDocument(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; } } 

代码工作正常,对GetObjFromXmlDocument的调用产生了一个List集合。 但是,当XmlSerializer serializer = new XmlSerializer(typeof(T));时,我总是在mscorlib.dll中获得类型System.IO.FileNotFoundException的第一次机会exceptionXmlSerializer serializer = new XmlSerializer(typeof(T)); 被执行。

所以我进入了Debug / Exception并打开了Managed Debugging Assistants。 我在这一行得到了以下内容:

显示名称为“mscorlib.XmlSerializers”的程序集无法加载到ID为1的AppDomain的“LoadFrom”绑定上下文中。失败的原因是:System.IO.FileNotFoundException:无法加载文件或程序集’mscorlib.XmlSerializers ,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’或其依赖项之一。 该系统找不到指定的文件。 文件名:’mscorlib.XmlSerializers,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089′

有人可以解释为什么会这样吗? 我可以对UserSetting类做些什么来使问题消失吗? 该应用程序对性能非常敏感,我宁愿没有例外。

微软说 :

XmlSerializer尝试加载预生成的序列化程序,以避免动态编译序列化代码。 没有简单的方法来检查“将通过Assembly.Load()调用找到程序集”,它将在XmlSerializer中复制Fusion路径搜索和加载程序逻辑。

当无法找到“预生成的序列化程序”时,看起来像是在XmlSerializer中抛出并处理了FileNotFoundexception,这将导致生成序列化代码。

对于我所选择的几个Visual Studio项目,这是一个烦恼,我更喜欢为BindingFailureSystem.IO.FileNotFoundException禁用break on exception。

在Visual Studio中:Ctl-D,Ctl-E用于“例外”对话框:

1)取消选中Managed Debugging Assistants下的BindingFailure

2)取消选中Common Language Runtime Exceptions下的System.IO.FileNotFoundException。

啊,那更好:-)