. Net binary deserialization Problems

Source: Internet
Author: User
I made some changes to the dynamic entity over the past few days, and my colleagues made an error when running the code. After careful debugging, I found that a callback was written during deserialization. However, when a callback occurs, the object has not been fully initialized. Specifically:
I have a class: dependencymetadatabase, which contains a method: [Ondeserialized ()]
Private   Void Ondeserialized (streamingcontext context) {
This. Ondeserialization (Null);
}

In addition, the derived class contains an internal variable:PrivateDependencypropertydata _ data;

Unfortunately, the _ DATA variable is used for callback during deserialization. The trace finds that when the callback occurs, _ data is not null, but _ data is in the internal variable, all value types (including string) have been filled, but all referenced types (class) are null, resulting in an error in my callback.
View msdn and get the following text:

the object will be completely re-constructed, and calling methods during deserialization may produce adverse side effects, because The called method may be referenced by objects that are not deserialized during this call . If the deserialization class is implemented the entire object graph is automatically called after the object graph is deserialized. method . At this time, all referenced sub-objects have been completely restored. A hash table is a typical example of a class that is difficult to deserialize without using an event listener. It is very easy to retrieve key/value pairs during deserialization, but adding these objects back to the hash table may cause some problems because classes derived from the hash table cannot be deserialized. Therefore, it is not recommended to call a method for the hash table at this stage.

therefore, I used ideserializationcallback to implement the callback function. In fact, I used the ideserializationcallback Method for another class, but why is this class not used later? At that time, I encountered a problem. Suppose A implemented ideserializationcallback , and B also implemented it. Unfortunately, when a references B and deserializes it, the ideserializationcallback method cannot be called first, therefore, when ideserializationcallback of a calls data of B, it is likely that B does not call ideserializationcallback , cause Program errors.
so at that time, I used the Final Solution implements the callback mode. However, before a calls B, check whether B has called callback. If not, call it in advance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.