与 ConfigurationProperty 属性一起使用时,属性类型的隐式约定是什么?

发布于 2024-10-17 21:22:45 字数 845 浏览 1 评论 0原文

例如,我想序列化和反序列化一个 System.Version 对象作为我的应用程序自定义配置部分的一部分。我尝试使用以下属性声明来执行此操作:

public class ConfigElement : ConfigurationElement
{
    [ConfigurationProperty("ver", IsRequired = false, DefaultValue = "1.2.4.8")]
    public Version Ver
    {
        get { return (Version)this["ver"]; }
        set { this["ver"] = value; }
    }
}

不幸的是,尝试序列化或使用此属性(带或不带 DefaultValue)会产生以下异常消息。

System.Configuration.ConfigurationErrorsException:属性“ver”的值无法转换为字符串。错误是:无法找到支持“Version”类型的属性“ver”与字符串之间转换的转换器。

System.Version.ToString() 将对象写入一种众所周知的字符串格式,可由 System.Version.ctor(string) 使用,因此这种类型存在“转换器”似乎是可行的。相比之下,System.TimeSpan 类型具有类似的方法和功能(Parse 代替 .ctor(string)),并且该类型运行良好与配置系统(转换器必须已存在)。

我如何知道某个类型是否有合适的转换器?这种类型必须满足什么契约(隐式的或其他的)?

As an example, I would like to serialize and deserialize a System.Version object as part of my application's custom configuration section. I am attempting to do so with the following property declaration:

public class ConfigElement : ConfigurationElement
{
    [ConfigurationProperty("ver", IsRequired = false, DefaultValue = "1.2.4.8")]
    public Version Ver
    {
        get { return (Version)this["ver"]; }
        set { this["ver"] = value; }
    }
}

Unfortunately, attempting to serialize or use this property (with or without the DefaultValue) yields the following exception message.

System.Configuration.ConfigurationErrorsException : The value of the property 'ver' cannot be converted to string. The error is: Unable to find a converter that supports conversion to/from string for the property 'ver' of type 'Version'.

System.Version.ToString() writes the object to a well-known string format which is consumable by System.Version.ctor(string), so it seems feasible for a "converter" to exist for this type. Comparably, the System.TimeSpan type has similar methods and functions (Parse in-place of .ctor(string)) and the type works well with the configuration system (a converter must already exist).

How do I know if a type has a suitable converter? What contract (implicit or otherwise) must such a type satisfy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疑心病 2024-10-24 21:22:45

为了使 ConfigurationProperty 正常工作,所使用的类型必须与 TypeConverter 关联 比知道如何从字符串转换。 ConfigurationProperty 确实有一个 Converter 属性,但是可惜,它是只读的。而且,这真的很不幸,Version 也没有声明隐式 TypeConverter。

您可以做的是将 TypeConverterAttribute 添加到以编程方式创建 Version 类,它将解决所有这些问题。因此,在访问配置之前,您基本上需要在程序中调用此行一次:

TypeDescriptor.AddAttributes(typeof(Version), new TypeConverterAttribute(typeof(VersionTypeConverter)));
// ... you can call configuration code now...

使用以下定制的 VersionTypeConverter:

public class VersionTypeConverter : TypeConverter
{
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return new Version((string)value);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;

        return base.CanConvertFrom(context, sourceType);
    }
}

For the ConfigurationProperty to work, the type used must be associated with a TypeConverter than knows how to convert from a string. ConfigurationProperty does have a Converter property, but alas, it's read-only. And, that's really bad luck, Version does not have an implicit TypeConverter declared either.

What you can do though, is add a TypeConverterAttribute to the Version class programmatically, and it will work around all these issues. So you need to basically call this line once in your program before accessing the configuration:

TypeDescriptor.AddAttributes(typeof(Version), new TypeConverterAttribute(typeof(VersionTypeConverter)));
// ... you can call configuration code now...

with the following custom-made VersionTypeConverter:

public class VersionTypeConverter : TypeConverter
{
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return new Version((string)value);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;

        return base.CanConvertFrom(context, sourceType);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文