WCF deserialization, does not call constructors, how to resolve class initialization problems

Source: Internet
Author: User

WCF deserialization, does not call the constructor, how to solve the class initialization problem? Encountered this problem, found some information to finally solve the problem. The key is two features:

[OnDeserializing]

[OnDeserialized]

Check the code:

   [DataContract] public class Mydatatype {[DataMember] private list<int> nval = new List<in        T> ();        Public Mydatatype () {nval.add (12);  } [ondeserializing] void BeginInit (StreamingContext sc) {Console.WriteLine ("Same to begin        Constructor "); } [ondeserialized] void EndInit (StreamingContext sc) {Console.WriteLine ("Same to end Cons        Tructor ");            } [DataMember] public int Initmemberdata {get {return 0;}                set {Object obj = this;            Console.WriteLine ("Initmemberdata:set.");        }} private int intv;            [DataMember] public int int {get {return intv;                } set {Console.WriteLine ("Int:set.");        if (nval = = null) Intv = value;    }} string _str;            [DataMember] public string String {get {return _str;}                set {Console.WriteLine ("String:set.");             _str = value;        }} long _long;            [DataMember] public long Long {get {return _long;}                set {Console.WriteLine ("Long:set.");             _long = value; }        }           }

Output when deserializing:

WCF deserialization, does not call constructors, how to resolve class initialization problems

Related Article

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.