Setter is called before constructor
I am debugging a code and one strange thing occurs. There is a string property say MyProperty. When I add breakpoint at the setter of the property and at the default constructor ( The only constructor ) the setter is called first. I dont know what is going on at other level of the code but this seem开发者_如何学Cs strange to me in any case. All the members are non static. The class has a [DataContract] attribute and members are [DataMember]. Can anybody explain this?
I cannot share any more code than this. Also the code is in a Silverlight project.
Is this during WCF deserialization? In WCF deserialization, the constructor isn't called at all. That's by design. WCF objects are designed to be data transport objects, and any logic that resides behind setters and getters is dangerous at best. Chances are, you're seeing this behavior and concerned about it because you have something going on in business logic that resides in the setter. To work around this, I'd suggest decorating only automatic properties and fields with the [DataMember] attribute, which will prevent any other logic from being triggered. Also, be careful of any logic that would be performed in the constructor, that logic won't be executed during deserialization.
精彩评论