不可变类型作为配置属性 [英] Immutable types as configuration properties

查看:239
本文介绍了不可变类型作为配置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用不可变类型作为.NET配置API的配置属性?

Is it possible to use immutable types as configuration properties with .NET's configuration API?

假设我有一个名为MyClass的不可变类型:

Let's say I have an immutable type called MyClass:

public class ImmutableClass
{
    private readonly int value;

    public ImmutableClass(int value)
    {
        this.value = value;
    }

    public int Value
    {
        get { return this.value; }
    }
}

我想使用此类型作为配置属性:ConfigurationSection:

I would like to use this type as a configuration property in a ConfigurationSection:

public class MyConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("foo")]
    public ImmutableClass Foo
    {
        get { return (ImmutableClass)base["foo"]; }
        set { base["foo"] = value; }
    }
}

当我这样做时,和反序列化MyConfigurationSection,因为它抛出此异常:

When I do this, I can't serialize and deserialize MyConfigurationSection because it throws this exception:


System.Configuration.ConfigurationErrorsException:属性criteria的值不能转换为字符串。错误是:无法找到一个转换器,支持类型为FindCriterion的属性标准的字符串转换。

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

如果我从ConfigurationElement派生ImmutableClass,当我尝试序列化和反序列化配置部分时,我得到这个异常:

If I derive ImmutableClass from ConfigurationElement, I get this exception when I try to serialize and deserialize the configuration section:


System.Reflection.TargetInvocationException :调用的目标抛出了异常。 ---> System.Reflection.TargetInvocationException:调用的目标抛出了异常。 ---> System.MissingMethodException:没有为这个对象定义无参数的构造函数。

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: No parameterless constructor defined for this object.

我可以完全控制ImmutableClass,

I have full control over ImmutableClass, so I can implement whichever interface is needed.

有没有什么方法可以挂钩到配置API,这样ImmutableClass可以用这种方式?

Is there any way I can hook into the configuration API so that ImmutableClass can be used in this way?

推荐答案

您可以使用一些 TypeConverters 来帮助进程(链接到System.Configurations.ConfigurationConverter)。

You might be able to use some TypeConverters to help the process along (link to System.Configurations.ConfigurationConverter).

真正的问题是,你为什么要让自己这么多麻烦?只有一些时候,它更好地躺下来拿起来比起来打架。框架的某些部分是令人沮丧的,如果你试图做任何方式,而不是为你规定的具体一个....

The real question is why do you want to cause yourself so much trouble? There are just some times where its better to lay back and take it than put up a fight. Some parts of the framework are frustrating if you try to do it any way other than the specific one laid out for you....

这篇关于不可变类型作为配置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆