Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom types in settings

Tags:

c#

.net

How can I have my own types in Settings.

I succeed to have them in settings sheet, but the issue is that I can't set default values. And the problem is that I can't see the setting in app.config.

like image 369
NDeveloper Avatar asked Jun 24 '11 14:06

NDeveloper


1 Answers

If I interpret your question right, you have a custom type, let's call it CustomSetting, and you which to have a setting in your Settings.settings file of that type, and specify a default value for that setting using app.config or Visual Studio's settings UI.

If that is what you want to do, you need to provide a TypeConverter for your type that can convert from a string, like this:

[TypeConverter(typeof(CustomSettingConverter))]
public class CustomSetting
{
    public string Foo { get; set; }
    public string Bar { get; set; }

    public override string ToString()
    {
        return string.Format("{0};{1}", Foo, Bar);
    }
}

public class CustomSettingConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if( sourceType == typeof(string) )
            return true;
        else
            return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        string stringValue = value as string;
        if( stringValue != null )
        {
            // Obviously, using more robust parsing in production code is recommended.
            string[] parts = stringValue.Split(';');
            if( parts.Length == 2 )
                return new CustomSetting() { Foo = parts[0], Bar = parts[1] };
            else
                throw new FormatException("Invalid format");
        }
        else
            return base.ConvertFrom(context, culture, value);
    }
}

Some background information

TypeConverter is behind a lot of the string conversion magic in the .Net framework. It's not just useful for settings, it's also how the Windows Forms and Component designers convert values from the property grid to their target types, and how XAML converts attribute values. Many of the framework's types have custom TypeConverter classes, including all the primitive types, but also types like System.Drawing.Size or System.Windows.Thickness and many, many others.

Using a TypeConverter from your own code is very easy, all you need to do is this:

TypeConverter converter = TypeDescriptor.GetConverter(typeof(TargetType));
if( converter != null && converter.CanConvertFrom(typeof(SourceType)) )
    targetValue = (TargetType)converter.ConvertFrom(sourceValue);

Which source types are supported varies, but string is the most common one. It's a much more powerful (and unfortunately lesser known) way of converting values from strings than the ubiquitous but less flexible Convert class (which only supports primitive types).

like image 181
Sven Avatar answered Nov 03 '22 11:11

Sven