I'm writing a plug-in for an application where I have a custom class that attributes the native objects of the program. The API allows me to read and write keyed strings directly to and from the objects in the native file. So rather then reading and writing to private fields in the Get and Set accessors of each property I'm reading and writing these user strings to and from the native objects in the program.
The user accesses these properties through a PropertyGrid control. So far this is all working fine with simple properties except now I need to store a custom collection (FloorCollection) of a custom object type (Floor).
So I've setup a property using XmlSerializer. The GET accessor reads the user string from the native objects, and then the string is deserialized into an instance of FloorCollection. The SET accessor does the reverse by reading in an instance of a FloorCollection, and serializing it to the user string.
Here's what my property looks like:
public FloorCollection Floors
{
get
{
string oldString = this.GetBaseObjUserString("CPFloors");
if (oldString == null) return null;
XmlSerializer serializer = new XmlSerializer(typeof(FloorCollection));
StringReader strReader = new StringReader(oldString);
FloorCollection newCollection =
(FloorCollection)serializer.Deserialize(strReader);
return newCollection;
}
set
{
FloorCollection newCol = value;
if (newCol == null) return;
//serializes new collection
XmlSerializer serializer = new XmlSerializer(typeof(FloorCollection));
StringWriter sw = new StringWriter();
serializer.Serialize(sw, newCol);
string newString = sw.ToString();
this.SetBaseObjUserString("CPFloors", newString);
}
}
I can open this collection property through my PropertGrid in the default collection editor. However, the default collection editor only edits the collection at the reference provided by the GET accessor, rather then calling the SET accessor to save it back to the object. So I can edit the collection in the default editor, but when I close and reopen the editor the collection will not have changed because the SET accessor is never called to serialize the collection back to the user string in the native object.
Can I modify the default collection editor to call the set accessor of the property? If I setup a custom UITypeEditor for the property is there a way to have it save back to the property using the property set accessor? Or is there some other way to force code that is accessing the collection property to GET and SET the property by value rather then reference?