Persisting custom dictionary property

N

Nels P. Olsen

I have a custom ASP control (an extension of DataSourceControl) that contains
a custom property, which contains a custom dictionary property. I can't get
the custom dictionary property to persist correctly from my custom designer.
The code looks like this:

[Designer(typeof(MyCustomControlDesignerType)]
[ParseChildren(true)]
[PersistChildren(false)]
[Serializable]
public class MyCustomControlType : System.Web.UI.DataSourceControl
{

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public MyCustomPropertyType CustomProperty {get { ... } set {...}}
}

public class MyCustomControlDesignerType :
System.Web.UI.Design.DataSourceDesigner
{
...
}

[ParseChildren(true)]
[PersistChildren(false)]
[Serializable]
public MyCustomPropertyType
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[PersistenceMode(PersistenceMode.Attribute)]
public string SomeShortTextProperty {get {...} set {...}}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public string SomeLongTextProperty {get {...} set {...}}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public MyCustomDictionaryType CustomDictionaryProperty {get {...} set
{...}}
}

[Serializable]
public MyCustomDictionaryType : IDictionary, ...
{
public MyCustomDictionaryValueType this[string key] {get {...} set {...}}

// Implementation of IDictionary members
// enforces dictionary values of type MyCustomDictionaryValueType
// MyCustomDictionaryValueType.Name is automatically used as the key
}

[ParseChildren(true)]
[PersistChildren(false)]
[Serializable]
public MyCustomDictionaryValueType
{
// The Name property serves as the dictionary key
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[PersistenceMode(PersistenceMode.Attribute)]
public string Name {get {...} set {...}}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[PersistenceMode(PersistenceMode.Attribute)]
public Type Type {get {...} {set {...}}
}

When I save changes to an instance of my control in my designer, the
dictionary entries are persisted in the ASP markup like this:

<myNamespace:MyCustomControlType>
<CustomProperty SomeShortTextProperty="...">
<SomeLongTextProperty>
...
</SomeLongTextProperty>
<CustomDictionaryProperty>
<dictionaryentry key="theKey" value="theValueToString">
...
</CustomDictionaryProperty>
</CustomProperty>
</myNamespace:MyCustomControlType>

I haven't been able to figure out a way to control the persisted ASP markup
of the dictionary entries. I've tried all kinds of things -- implementing
IXmlSerializable in my custom dictionary class, decorating my custom
dictionary value class with a custom type converter, etc. Nothing I do has
any effect.

How can I get the persisted ASP markup to look like this, which I can
deserialize:

<CustomDictionaryProperty>
<CustomDictionaryValue Name="theKey" Type="theAssemblyQualifiedName">
...
</CustomDictionaryProperty>

instead of this, which I can't deserialize:

<CustomDictionaryProperty>
<dictionaryentry key="theKey"
value="MyNamespace.MyCustomDictionaryValueType">
...
</CustomDictionaryProperty>
 

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

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top