OschtärEi OschtärEi - 1 year ago 120
C# Question

DialogPage - string array not persisted

I'm developing an extension for visual studio.

There I have an option page:

public class GeneralOptionsPage : DialogPage
[Description("Bla Foo Bla")]
public string[] Foos { get; set; }

[Description("Bar Foo Bar")]
public string Bar { get; set; }

property works perfectly and is persisted.

Property does also work (it even gives you a nice popup in the options page where you can enter one string per line), which means I can set it and also use it in my extension but it is not written to the registry/storage. When I close VS and open it again it's always empty.

Quote from MSDN:

The default implementation of DialogPage supports properties that have appropriate converters or that are structures or arrays that can be expanded into properties that have appropriate converters. For a list of converters, see the System.ComponentModel namespace. The Visual Studio Extensibility Samples manages int, string, and System.Drawing.Size properties.

In my understanding I'm using valid components from the

So what am I doing wrong? Do I have to treat arrays somehow differently?

Answer Source

You will need to implement and associate a custom TypeConverter for your Foos property.

There is no stock converter that covers this scenario, because when you are converting an array of strings to a string, you have to have some sort of delimiter, so you can reconstitute the array from the string. And that will vary depending upon the individual programmers application. Hence the need for a custom TypeConverter.

So you have to derive a new class from System.ComponentModel.TypeConverter, and then associate it with your Foos property. For example:

    [Description("Bla Foo Bla")]
    public string[] Foos { get; set; }

A quick internet search should pull up a few examples to get you pointed in the right direction.

The DialogPage.SaveSettingsToStorage loops through a collection of the page's PropertyDescriptors, retrieves each propertie's Converter, invokes CanConvertTo and CanConvertFrom to ensure the property can be converted to/from a string, then calls the converter's ConvertToInvariantString to persist the property to the registry value.

Conversely, the DialogPage.LoadSettingsfromStorage loops through the registry values, finds the PropertyDescriptor matching the property on the DialogPage, retrieves its Coverter, then calls CanCovertFrom to ensure the string can be converted back to that particular property type, and then calls the converter's ConvertFromIvariantString, and then assigns the value back to the property.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download