Assume that I need to store any array in the extension just freshly created from the template.
I just created new VSIX project, added VSPackage to it, then added option page grid (DialogPage
). Then I followed instructions from answers to a similar question: DialogPage
- string array not persisted.
And, for demonstration purposes, let's also add int[]
array and plain int
with custom type converter.
// [standard attributes]
[ProvideOptionPage(typeof(OptionPageGrid),
"My Category", "My Grid Page", 0, 0, true)]
public sealed class FooBarVSPackage : Package
{
// standard code
}
public class OptionPageGrid : DialogPage
{
// [typical attributes]
[TypeConverter(typeof(StringArrayConverter))]
public string[] Foos
{ get; set; }
// [typical attributes]
[TypeConverter(typeof(CustomIntConverter))]
public int Bar
{ get; set; }
// [typical attributes]
[TypeConverter(typeof(IntArrayConverter))]
public int[] Bazes
{ get; set; }
}
class StringArrayConverter : TypeConverter
{
// exact copy of code from similar question/answer mentioned above
}
public class IntArrayConverter : TypeConverter
{
private const string delimiter = "#@#";
// CanConvertFrom, ConvertTo, etc. overridden in similar fashion
}
public class CustomIntConverter : TypeConverter
{
// CanConvertFrom() overridden
// CanConvertTo() overridden
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var v = value as string;
return int.Parse(v.TrimStart('*'));
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var v = (int)value;
return v.ToString().PadLeft(25, '*');
}
}
When I edit those options, I can see that the converter really works:
But after I reopen it, two of the values gone! Only plain int
persisted:
There is also one strange thing: how and when TypeConverter
methods are called. CanConvertTo()
is never called during the whole session. CanConvertFrom()
and ConvertTo()
are called often and more or less in expected fashion. And ConvertFrom()
is called only when the string representation of the option is edited directly, i.e. it doesn't participate in loading/saving options at all!
I'm not sure, but it feels a bit like int
option is stored as int
and turned from/into string
only in options GUI, while array options just silently fail trying to do the same.
P.S.: If you want to directly play with the example personally, here is a GitHub repo with the example project in question: FooBarVSIXProject