gpt4 book ai didi

c# - 为什么会出现序列化错误?

转载 作者:太空狗 更新时间:2023-10-29 17:45:26 27 4
gpt4 key购买 nike

我有以下代码:

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; }
}

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

所以我进入调试/异常并打开托管调试助手。我在那条线上得到了以下信息:

The assembly with display name 'mscorlib.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified. File name: 'mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

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

最佳答案

Microsoft says :

XmlSerializer attempts to load pre-generated serializers to avoid compilation of the serialization code on the fly. There is no easy way to check for "will assembly be found by the Assembly.Load() call", it would be duplicating Fusion path search and loader logic in XmlSerializer.

当找不到“预先生成的序列化程序”时,似乎会抛出 FileNotFound 异常并在 XmlSerializer 中进行处理,这将导致生成序列化代码。

关于c# - 为什么会出现序列化错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3517470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com