Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

c# - custom types in settings

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...