In app.config I have custom section with custom element.
<BOBConfigurationGroup>
<BOBConfigurationSection>
<emails test="[email protected], [email protected]"></emails>
</BOBConfigurationSection>
</BOBConfigurationGroup>
For emails element I have custom type :
public class EmailAddressConfigurationElement : ConfigurationElement, IEmailConfigurationElement
{
[ConfigurationProperty("test")]
public string[] Test
{
get { return base["test"].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
set { base["test"] = value.JoinStrings(); }
}
}
But when I run my webApp, I get error :
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[]'.
Is there any solution to split string in getter?
I can get string value and then split it "manually" when I need array, but in some cases I can forget about it, so better to receive array from start.
JoinStrings - is my custom extension method
public static string JoinStrings(this IEnumerable<string> strings, string separator = ", ")
{
return string.Join(separator, strings.Where(s => !string.IsNullOrEmpty(s)));
}