3

情况是这样的:主项目A.和一个类库B.A引用B

项目 B 具有将被序列化的类。这些类在 A 中使用。现在,当从 Project AI 尝试序列化 B 中的对象时出现问题。抛出异常,表示 A 中的类无法序列化。这是奇怪的部分,因为在 BI 中的类中不能引用 A 中的那些。(将创建循环依赖项)。

我怎样才能找到问题?因为异常方法没有说明问题出现在哪里?

编辑:好的,我在Kent Boogaart 的小应用程序 的帮助下发现了问题:D。我在项目 A 的一个类中有一个 PropertyChanged 侦听器,它没有标记为 Serializable - 我不想这样标记它。(它会将该类序列化为正确的?)

我通过以下链接解决了事件问题:.NET 2.0 solution to serialization of objects that raise events。仍然存在问题,但它可能是类似的东西。

PS:来自Kent Boogaart的好工具

4

4 回答 4

10

我编写了一个名为sertool的工具,它会告诉您对象图中的哪些内容不能被序列化以及它是如何被引用的。

于 2008-10-08T10:44:52.877 回答
2

您首先需要将问题隔离到特定的类。然后您可以实现自定义序列化并对其进行调试以找到真正的问题。

只是一个简单的实现,让您逐步完成该过程:

using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

[Serializable]
public class Test : ISerializable
{
    private Test(SerializationInfo info, StreamingContext context)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Test));

        foreach (SerializationEntry entry in info)
        {
            PropertyDescriptor property = properties.Find(entry.Name, false);
            property.SetValue(this, entry.Value);
        }
    }

    [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter = true)]
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Test));

        foreach (PropertyDescriptor property in properties)
        {
            info.AddValue(property.Name, property.GetValue(this));
        }
    }
}

Kent 的工具看起来也很漂亮,毫无疑问,它会帮助你。

于 2008-10-08T10:51:04.237 回答
1

也许您来自 B 的对象正在使用不属于 A 或 B 的类/接口存储对来自 A 的对象的引用,例如,如果 B 使用对象 (System.Object) 引用从 A 存储对象

于 2008-10-08T10:43:29.337 回答
0

假设 TA 和 TB 是在 A 和 B 中定义的类型。假设在 B 或 B 和 A 引用的程序集中有一个接口 I。TA 实现了 I。TB 有一个名为 P 的类型 I 的公共可设置属性。

您现在可以这样做:

TB b = new TB();
b.P = new TA();

由于 TA 实现了 I,因此这是可能的。

现在您的对象图有一个类型的实例,该实例可能无法序列化并且来自程序集 A。

于 2008-10-08T11:19:40.387 回答