Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
267 views
Welcome To Ask or Share your Answers For Others

1 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...