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

C# 使用类型转换器进行应用程序设置时出错,c#,.net,visual-studio,settings,type-conversion,C#,.net,Visual Studio,Settings,Type Conversion,在阅读了一些文章之后,我想我已经知道了如何通过VisualStudioDesigner使用自定义类型作为应用程序设置 设计器很高兴地存储了我的字符串,但当我运行单元测试以查看是否可以实际使用该设置时,会收到一条错误消息 我在一些其他类型上遇到了序列化错误,这些类型构成了问题中的自定义类型——我不情愿地公开了几个属性设置器,从而消除了这些错误。但是文档建议,如果提供了类型转换器,将使用它来代替序列化。我必须为构成我想要设置的类型的每个类型提供类型转换器吗 干杯, 贝里尔 从设计器设置生成的代码 单

在阅读了一些文章之后,我想我已经知道了如何通过VisualStudioDesigner使用自定义类型作为应用程序设置

设计器很高兴地存储了我的字符串,但当我运行单元测试以查看是否可以实际使用该设置时,会收到一条错误消息

我在一些其他类型上遇到了序列化错误,这些类型构成了问题中的自定义类型——我不情愿地公开了几个属性设置器,从而消除了这些错误。但是文档建议,如果提供了类型转换器,将使用它来代替序列化。我必须为构成我想要设置的类型的每个类型提供类型转换器吗

干杯,
贝里尔

从设计器设置生成的代码 单元测试与错误 类型转换器 设置文件(已设置多个用户设置) 更新(已修复!)
答案是第一个看到我遗漏了谜题的哪一部分的人。提示#1-原始的TypeConverter代码很好。提示#2-System.ComponentModel非常强大

@Berryl-我已经开始工作了(做了一些修改,比如创建自己的TimeQuantity类)。您是否记得向TimeQuantity类添加TypeConverterAttribute

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

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

您的设置文件是什么样子的?@Chris。我在文章末尾发布了更多生成的代码,以及我在TypeConverter中使用InstanceDescriptor的最新更改。设置文件中的其他设置是可用的。@Chris。不,我毕竟不想要InstanceDescriptor。回到我的原始代码和错误。是的,福尔摩斯。这个属性就是我所缺少的。干杯
    [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"]));
        }
    }
}
[TypeConverter(typeof(TimeQuantityTypeConverter))]
public class TimeQuantity
{