c# – 使用类型转换器进行应用程序设置时出错

在阅读了
msdn docs的一些内容之后,我认为我已经想通过视觉工作室设计师如何使用自定义类型作为应用程序设置.

设计师快乐地存储我的字符串,但是当我运行单元测试以查看是否可以实际使用该设置时,我收到错误消息.

我在一些构成自定义类型的其他类型上遇到了序列化错误 – 我不情愿地将几个属性setter公之于众.但是文档建议如果提供类型转换器,它将用于代替序列化.我必须为构成我想要的类型的每种类型提供类型转换器吗?

干杯,
Berryl

从设计师设置生成代码

[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("8 hours")]
public global::Smack.Core.Lib.Domains.Temporal.TimeQuantity WorkQuota_Daily {
    get {
        return ((global::Smack.Core.Lib.Domains.Temporal.TimeQuantity)(this["WorkQuota_Daily"]));
        }
    }

单元测试和错误

    [Test]
    public void WorkQuota_Daily_CanRead() {
        var setting = Properties.Settings.Default.WorkQuota_Daily;
        Assert.That(setting, Is.EqualTo(TimeQuantity.Hours(8)));
    }
Test failed: System.ArgumentException : The property 'WorkQuota_Daily' could not be created from it's default value. 
Error message: There is an error in XML document (1, 1).
at System.Configuration.SettingsPropertyValue.Deserialize()
at System.Configuration.SettingsPropertyValue.get_PropertyValue()
at System.Configuration.SettingsBase.GetPropertyValueByName(String propertyName)
at System.Configuration.SettingsBase.get_Item(String propertyName)
at System.Configuration.ApplicationSettingsBase.GetPropertyValue(String propertyName)
at System.Configuration.ApplicationSettingsBase.get_Item(String propertyName)
C:\Users\Lord & Master\Documents\Projects\Smack\trunk\src\ConstructionAdmin.TestingSupport\Properties\Settings.Designer.cs(211,0): at Smack.ConstructionAdmin.TestingSupport.Properties.Settings.get_WorkQuota_Daily()
General\ApplicationSettingsTests.cs(22,0): at Smack.ConstructionAdmin.TestingSupport.General.ApplicationSettingsTests.WorkQuota_Daily_CanRead()

型转换器

public class TimeQuantityTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string)
        {
            var v = ((string)value).Split();
            var amt = v[0];
            var unit = v[1];
            var timeSliceFactory = new TimeSliceFactory();
            var map = TimeSliceFactory.GetUnitMap(timeSliceFactory);
            var key = unit.ToLowerInvariant();
            if (!map.ContainsKey(key)) throw new ArgumentException(string.Format("There is no time slice unit key fpr '{0}", key));
            return new TimeQuantity(amt, map[key]);
        }
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string)) {
            return string.Format("{0} {1}", ((TimeQuantity) value).Amount, ((TimeQuantity) value).Unit.PluralForm);
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

设置文件(省略了几个用户设置)

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default {
        get {
            return defaultInstance;
        }
    }

    [global::System.Configuration.ApplicationScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("8 hours")]
    public global::Smack.Core.Lib.Domains.Temporal.TimeQuantity WorkQuota_Daily {
        get {
            return ((global::Smack.Core.Lib.Domains.Temporal.TimeQuantity)(this["WorkQuota_Daily"]));
        }
    }

    [global::System.Configuration.ApplicationScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("Monday")]
    public global::System.DayOfWeek StartDay {
        get {
            return ((global::System.DayOfWeek)(this["StartDay"]));
        }
    }
}

更新(固定!)

回答第一个看到我失踪的难题部分的人.提示#1 – 原始的TypeConverter代码很好.提示#2 – System.ComponentModel非常强大!

最佳答案 @Berryl – 我得到了这个工作(进行了一些修改,比如创建我自己的TimeQuantity类).您是否记得在TimeQuantity类中添加TypeConverterAttribute?

[TypeConverter(typeof(TimeQuantityTypeConverter))]
public class TimeQuantity
{

如果在CanConvertFrom(…)和ConvertFrom(…)方法中设置断点,则应在读取属性时看到它们.如果没有TypeConverter属性,我会得到相同的错误.

点赞