开发者

C# - Event handling goes weird after I use serialization with my program

开发者 https://www.devze.com 2022-12-11 22:23 出处:网络
I have object A and object B deserialized from binary files. B has a private method which is used as a callback function and does some manipulation on a private data member when A raise an event. To c

I have object A and object B deserialized from binary files. B has a private method which is used as a callback function and does some manipulation on a private data member when A raise an event. To clarify the basic structure:

Class A
{
    private static A instance;
    public static A GetInstance(){...};
    private A(){}

    public delegate void SomeCallback(Arg a);
    public event SomeCallback doCallback;

    ...
}

Class B
{
    private Dictionary<., .> dict;

    public B()
    {
        A.GetInstance().doCallback += new A.SomeCallback(ManipulateDict);
        ...
    }

    private void ManipulateDict(Arg a){...} //breakpoint here

    public void PrintDict(){...}        
}

After A and B is deserialized, whenever A raise event doCallback, I can see the breakpoint line(ManipulateDict) will be executed as I'm expecting. However, the strange thing is, it will manipulate on a 'dict' which has a different memory address with the object's, which means, even if ManipulateDict 'successfully' updated some data in dict, the 开发者_Python百科other methods, say, PrintDict, still don't see the changes in dict.

If I don't use serialization, this won't happen and it behaves just right. But as serialization is introduced to this program, things goes weird. Am I doing something wrong? Who can explain this?

Thank you very much!


I would think that when you serialize B, it preserves the memory location of the original A.SomeCallback. When you deserialize A, the reconstituted object is now in a different location, and so SomeCallback is in a new location as well. The deserialized B is still calling SomeCallback in the old A location which would still be referring to the original data. Maybe you need to re-connect the delegate after deserialization.


What method of serialization are you using? I suspect that you are using something like XMLSerializer which is creating a new copy of each object every time it appears in the object graph. This means that you get multiple independent copies of the object when you deserialize. If you use SoapFormatter instead you should get just one object, which will solve the problem.

Update: Most deserialization methods call the default constructor. Your default constructor is creating new objects. This could also be a problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号