Why does ShouldSerialize behave this way for a PropertyGridView?

T

The Tarm

hi,
I have two classes, one base : Item and the other derived Group. Group is
derived from Item. Item has a bunch of properties that are exposed through
a PropertyGrid.SelectedObject. Now each of these properties has a
ShouldSerializeXX for them that turns the property into bold font whenever
ShouldSerializeXX return true.

Right now, in my code Item contains some properties and Group contains
some.
I expose all the properties through the Group type object. What happens
unfortunately here is that ShouldSerializeXX is not called on the Item
properties because they are in the base class. Am i missing something? Any
help would be great.

-suresh
 
M

Marc Gravell

What framework version? It looks fine for me in 2.0 (see below). Are
the property and the ShouldSerialize... in the same class? Any
DefaultValue etc? The only other thing I can think of is some obscure
PropertyDescriptor implementation (using ICustomTypeDescriptor or
TypeDescriptionProvider) that is returning a PropertyDescriptor with
different rules, but you didn't mention this (and it wouldn't happen
by accident).

Working example (string in base, int in sub, bool (in base) toggles
ShouldSerialize...):

Marc

using System;
using System.Windows.Forms;

class SomeBase {
private string someValue = "abc";
public string SomeValue {
get { return someValue; }
set { someValue = value; }
}
private bool someBool = false;
public bool SomeBool {
get { return someBool; }
set { someBool = value; }
}
private bool ShouldSerializeSomeValue() {
return SomeBool;
}
}

class SomeSub : SomeBase {
private int someOtherValue = 17;
public int SomeOtherValue {
get { return someOtherValue; }
set { someOtherValue = value; }
}
private bool ShouldSerializeSomeOtherValue() {
return !SomeBool;
}
}

static class Program {
static void Main() {
using (Form f = new Form())
using (PropertyGrid grid = new PropertyGrid()) {
f.Controls.Add(grid);
grid.Dock = DockStyle.Fill;
grid.SelectedObject = new SomeSub();
Application.Run(f);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top