Serialization issue

The situation is this: Main project A. and class library B. References B

Project B has classes to be serialized. Classes are used in A. Now the problem arises when I try to serialize objects from B. from Project A. An exception is fixed where it says that a class from A cannot be serialized. This is the weird part, since in classes in B I cannot refer to those in A. (a circular dependency will be created).

How can I track the problem? because the exception method does not say where the problem appeared?

Edit: Well, I found the problem with the small Kent Boogaart : D application . I have a PropertyChanged listener in a class in project A that is not marked Serializable - and I don't want to flag it. (will it serialize this class to the right?)

I solved the problem with the event by following the link: .NET 2.0 solution for serializing objects that raise events . There is still a problem, but probably something similar.

PS: Great tool from Kent Boogaart

+3
source share
4 answers

I wrote a tool called sertool that tells you what cannot be serialized on your object graph and how it refers.

+8

. , .

, :

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 , , .

+2

, B A, /, A B. , B A (System.Object)

+1

Let it be said that TA and TB are types defined in and B. Let it be said that the interface is either in B or in the assembly of both B and A. TA implements I. TB has a public, settable property of type I with the name P.

Now you can do this:

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

Since TA implements I, this is possible.

Your graphic now has an instance of a type that cannot be serializable and comes from assembly A.

0
source

Source: https://habr.com/ru/post/1698030/


All Articles