|
I have a little problem working with the ShouldSerializeMyProperty methods to tell the code serializer of the designer when to serialize my property. I need some complex conditions so I cannot use the DefaultValueAttribute.
I tried the following code:
public class MyForm : Form { public MyForm() { this.extraColor = Color.Red; this.extraValue = 0; this.layerData = new LayerData(); }
public Color ExtraColor { get { return this.extraColor; } set { this.extraColor = value; } } bool ShouldSerializeExtraColor() { Color color; if (this.extraValue == 0) color = Color.Red; else color = Color.Blue; return !(this.extraColor == color); }
public int ExtraValue { get { return this.extraValue; } set { this.extraValue = value; } } bool ShouldSerializeExtraValue() { return !(this.extraValue == 0); }
private int extraValue; private Color extraColor; }
Then I inherited MyForm in the following way:
public class Form1 : MyForm { ... }
When I opened Form1 in the designer and changed ExtraValue to 1 (from 0) then it gets serialized, but ExtraColor doesn't (it should serialize with a value of Red). Then I changed ExtraColor to Blue and it gets serialized (though it shouldn't). It looks like the ShouldSerializeMyProperty is not executed dynamically, but hardcoded or something. The most interesting of all is that I tried doing the same thing with a Panel (inheriting Panel to create MyPanel, and then MyPanel to create Panel1) and it worked as it should when I place a Panel1 on a Form and change the properties as explained before.
I also tried to attach another Visual Studio as a debugger to the serialization process. I put a breakpoint inside the ShouldSerializeExtraColor method, and when I tried it with the Panel version it gets called and returned the correct boolean value. When I tried it with the Form (as in the previous code) I found out that the ShouldSerializeExtraColor doesn't even get called !?!?!?!
I really would appreciate help with this problem. Please note that the previous code is just an example of what I need to do.
Uriel |