gpt4 book ai didi

c# - C# 中的 "There was an error reflecting type"

转载 作者:太空宇宙 更新时间:2023-11-03 14:23:05 35 4
gpt4 key购买 nike

我需要序列化和异常,我创建了一个可序列化的类

[Serializable]
public class MessageException : Exception
{
public string Exception
{ get; set; }
public MessageException()
{ }
public MessageException(System.Exception ex)
{
this.Exception = ex.Message.ToString();
}
}

我尝试在发生以下异常时调用该类

catch (Exception ex)
{
MessageException exception = new MessageException(ex);
var exSerializer = new XmlSerializer(typeof(MessageException));


var writer = new StringWriter();
exSerializer.Serialize(writer, exception);
writer.Close();
Compair2Files(writer.ToString(), BaseString);
}

它在第二行失败“var exSerializer = new XmlSerializer(typeof(MessageException));”

我实际上想做的是,当我捕获到异常时,我想将其序列化,然后将其存储到我的单元测试程序的文件中。你能帮我吗提前致谢日语

最佳答案

您不能使用 XmlSerializer 序列化异常。
您需要使用二进制序列化。

基础 Exception 类已经是 [Serializale],所以你的类没用。
这也是错误的;你忽略了堆栈跟踪。

关于c# - C# 中的 "There was an error reflecting type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4809651/

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