2008-10-08 93 views
3

的情况是这样的:主营项目A.和类库B. A引用乙序列化问题

项目B具有将被序列化的类。这些类在A中使用。现在,问题出现在来自项目A的时候,我尝试从B序列化对象。抛出一个异常,说A中的类不能被序列化。这是奇怪的部分,因为在B的类中我不能引用A中的那些(将创建循环依赖)。

我该如何追踪问题?因为异常方法没有说出问题出现在哪里?

编辑: 好吧,我发现的肯特Boogaart的小的应用程序的帮助下,问题:d。我在项目A的类中有一个PropertyChanged监听器,它没有标记为可序列化 - 我不想标记它。 (它会将该类序列化为正确的?)

我已通过以下链接解决了该事件的问题:.NET 2.0 solution to serialization of objects that raise events。 仍然有一个问题,但它可能类似。

PS:伟大的工具,从肯特Boogaart

+0

你在一个项目一个WebService做什么? – ullmark 2008-10-08 10:41:55

+0

请发布例外消息。 – FantaMango77 2008-10-08 10:42:28

回答

8

我已经写了一个名为sertool的工具,它会告诉您什么在您的对象图不能被序列化以及它如何被引用。

1

也许从B您的目标是从使用类/接口不属于A或B的一部分存储对象的引用,例如,如果B使用对象(System.Object)从A存储对象参考

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

肯特的工具看起来也很漂亮,并且将毫无疑问地帮助你。

0

假设TA和TB是在A和B中定义的类型。假设在B或B和A引用中有一个接口I。 TA实现一TB有I型命名为P.

的公开,可设置属性,现在你可以这样做:

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

由于TA实现了我,这是可能的。

现在你的对象图有一个类型的实例可能不序列化和来自组件A