简体   繁体   English

C#中的序列化异常

[英]Serialization Exception in C#

Serialization exception occurs when I try to serialize a class in C#, cause it holds a reference to a non serializable class. 当我尝试对C#中的类进行序列化时,会发生序列化异常,原因是它持有对不可序列化类的引用。 The problem is that the class I want to serialize is very complicated, and contains a lot of references to other complicated classes, and I can't find the reference to the problematic class in this huge amount of code. 问题在于我要序列化的类非常复杂,并且包含对其他复杂类的大量引用,而在如此大量的代码中我找不到对有问题的类的引用。 Is there a way to find out where is the property or filed with the non serializable class is? 有没有办法找出该属性或非可序列化类在何处? Perhaps somewhere in the exception details? 也许在异常细节中的某个地方? I didn't find something like it. 我没有找到类似的东西。

Thanks :) 谢谢 :)

You should start examining the exception's stack trace. 您应该开始检查异常的堆栈跟踪。

If necessary, configure Visual Studio to stop on any thrown exceptions (Debug -> Exception -> Check Thrown checkbox column for Common Language Runtime Exceptions) 如有必要,将Visual Studio配置为停止任何引发的异常(“调试”->“异常”->“检查抛出”复选框以查看“公共语言运行时异常”)

That should stop Visual Studio at the exact place where the exception was thrown and that will give you a stack trace wich you can examine. 这应该将Visual Studio停在引发异常的确切位置,这将为您提供可以检查的堆栈跟踪。

If that is not enough and depending on how your serializable classes are created, you can try running your assembly trough sgen.exe to create another assembly responsible for the serialization. 如果这还不够,并且取决于可序列化类的创建方式,则可以尝试通过sgen.exe运行程序集来创建另一个负责序列化的程序集。

sgen.exe has na option “/k” for keeping the source code. sgen.exe没有用于保留源代码的选项“ / k”。 You can add this code to a project from where you should be able to breakpoint into it. 您可以将此代码添加到项目中,您应该可以在其中断点。

Try to get the information from the exception by "catch (Exception exceptionObject) and wrapping the serialization call in a try block (opening a file in this example): 尝试通过“捕获”(ExceptionExceptionObject)并从try块中包装序列化调用(在此示例中打开文件)来从异常中获取信息:

try 
{
    FileStream inputFileStream = 
      new FileStream(aSaveFileNameFullPath, FileMode.Open, FileAccess.Read);
}
catch (Exception exceptionObject) 
{
    displayStandardExceptionInfo(exceptionObject, "Could not open file " + aSaveFileNameFullPath);
}

displayStandardExceptionInfo() is defined as: displayStandardExceptionInfo()定义为:

public static void displayStandardExceptionInfo(
  ref System.Exception anExceptionObject, ref string aStartText)
{
    string errMsg1 = 
      "Error: " + anExceptionObject.ToString() + "\n";
    string errMsg2 =
      "\n" + "Message: " + anExceptionObject.Message + "\n";

    string errMsg3 = "";
    if (anExceptionObject.InnerException != null)
    {
        errMsg3 = 
          "Extra info: " + 
          anExceptionObject.InnerException.Message +
          "\n";
    }
    string errMsg =
      aStartText + "\n" + "\n" + errMsg1 + errMsg2 + errMsg3;
    System.Windows.Forms.MessageBox.Show(errMsg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM