将 appsetting 值从字符串解析为字符串数组
Parse appsetting value from string to array of strings
在 app.config 中,我有带有自定义元素的自定义部分。
<BOBConfigurationGroup>
<BOBConfigurationSection>
<emails test="test1@test.com, test2@test.com"></emails>
</BOBConfigurationSection>
</BOBConfigurationGroup>
对于电子邮件元素,我有自定义类型:
public class EmailAddressConfigurationElement : ConfigurationElement, IEmailConfigurationElement
{
[ConfigurationProperty("test")]
public string[] Test
{
get { return base["test"].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
set { base["test"] = value.JoinStrings(); }
}
}
但是当我 运行 我的 webApp 时,我得到错误:
The value of the property 'test' cannot be parsed. The error is: Unable to find a converter that supports conversion to/from string for the property 'test' of type 'String[]'.
在getter中有拆分字符串的解决方案吗?
当我需要数组时,我可以获取字符串值然后拆分它"manually",但在某些情况下我可以忘记它,所以最好从一开始就接收数组。
JoinStrings - 是我的自定义扩展方法
public static string JoinStrings(this IEnumerable<string> strings, string separator = ", ")
{
return string.Join(separator, strings.Where(s => !string.IsNullOrEmpty(s)));
}
您可以添加一个 TypeConverter
以在 string
和 string[]
之间进行转换:
[TypeConverter(typeof(StringArrayConverter))]
[ConfigurationProperty("test")]
public string[] Test
{
get { return (string[])base["test"]; }
set { base["test"] = value; }
}
public class StringArrayConverter: TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string[]);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return ((string)value).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
return value.JoinStrings();
}
}
考虑如下方法:
[ConfigurationProperty("test")]
public string Test
{
get { return (string) base["test"]; }
set { base["test"] = value; }
}
public string[] TestSplit
{
get { return Test.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
}
其中 TestSplit
是您在代码中使用的 属性。
在 app.config 中,我有带有自定义元素的自定义部分。
<BOBConfigurationGroup>
<BOBConfigurationSection>
<emails test="test1@test.com, test2@test.com"></emails>
</BOBConfigurationSection>
</BOBConfigurationGroup>
对于电子邮件元素,我有自定义类型:
public class EmailAddressConfigurationElement : ConfigurationElement, IEmailConfigurationElement
{
[ConfigurationProperty("test")]
public string[] Test
{
get { return base["test"].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
set { base["test"] = value.JoinStrings(); }
}
}
但是当我 运行 我的 webApp 时,我得到错误:
The value of the property 'test' cannot be parsed. The error is: Unable to find a converter that supports conversion to/from string for the property 'test' of type 'String[]'.
在getter中有拆分字符串的解决方案吗?
当我需要数组时,我可以获取字符串值然后拆分它"manually",但在某些情况下我可以忘记它,所以最好从一开始就接收数组。
JoinStrings - 是我的自定义扩展方法
public static string JoinStrings(this IEnumerable<string> strings, string separator = ", ")
{
return string.Join(separator, strings.Where(s => !string.IsNullOrEmpty(s)));
}
您可以添加一个 TypeConverter
以在 string
和 string[]
之间进行转换:
[TypeConverter(typeof(StringArrayConverter))]
[ConfigurationProperty("test")]
public string[] Test
{
get { return (string[])base["test"]; }
set { base["test"] = value; }
}
public class StringArrayConverter: TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string[]);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return ((string)value).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
return value.JoinStrings();
}
}
考虑如下方法:
[ConfigurationProperty("test")]
public string Test
{
get { return (string) base["test"]; }
set { base["test"] = value; }
}
public string[] TestSplit
{
get { return Test.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
}
其中 TestSplit
是您在代码中使用的 属性。