When reading my config section, I want to call a method to convert the value specified to a ConsoleKey, if possible.
My config section is as follows:
<configuration>
<configSections>
<section name="SettingsContainer" type="Input.SettingsContainer, Input" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
</appSettings>
<SettingsContainer>
<Bindings>
<Binding KeyBinding="1" Description="Test description" />
</Bindings>
</SettingsContainer>
When reading in the KeyBinding
value, I want to attempt to convert this to a ConsoleKey
. In this case, the value in the configuration is 1
, and I want to convert this to ConsoleKey.NumPad1
.
My Configuration element is as follows:
class SettingsContainer : ConfigurationSection
{
[ConfigurationProperty("Bindings")]
public Bindings Bindings
{
get
{
return base["Bindings"] as Bindings;
}
}
}
[ConfigurationCollection(typeof(Binding), AddItemName = "Binding")]
public class Bindings : ConfigurationElementCollection, IEnumerable<Binding>
{
protected override ConfigurationElement CreateNewElement()
{
return new Binding();
}
protected override object GetElementKey(ConfigurationElement element)
{
var l_configElement = element as Binding;
if (l_configElement != null) return l_configElement.KeyBinding;
else return null;
}
public Binding this[int index]
{
get
{
return BaseGet(index) as Binding;
}
}
IEnumerator<Binding> IEnumerable<Binding>.GetEnumerator()
{
return (from i in Enumerable.Range(0, this.Count)
select this[i])
.GetEnumerator();
}
}
public class Binding : ConfigurationElement
{
[ConfigurationProperty("KeyBinding", IsRequired = true)]
public ConsoleKey KeyBinding
{
get
{
// This is where I'm attempting to pass my configuration value
// into my method, in order to return a ConsoleKey
return GetConsoleKeyFromString(base["KeyBinding"] as string);
}
set
{
base["KeyBinding"] = value;
}
}
[ConfigurationProperty("Description", IsRequired = true)]
public string Description
{
get
{
return base["Description"] as string;
}
set
{
base["Description"] = value;
}
}
private ConsoleKey GetConsoleKeyFromString(string keyStr)
{
Regex rex = new Regex("^[0-9]+$");
try
{
return (ConsoleKey)Enum.Parse(typeof(ConsoleKey), string.Format("NumPad{0}", keyStr), true);
}
catch
{
throw new ConfigurationErrorsException("Unknown value provided for KeyBinding");
}
}
}
I'm attempting to pass the KeyBinding
value into the GetConsoleKeyFromString
, in order to return the relevant ConsoleKey
.
The issue I'm having is that the value passed into this method is null
. How can I pass my KeyBinding
value from my app.config into my GetConsoleKeyFromString
, in order to convert the string value into a ConsoleKey?
EDIT
Just for completeness, I've updated my ConfigurationSection class to contain the full code. Additionally, I use the code below to attempt to get this configuration:
var Settings = (SettingsContainer)ConfigurationManager.GetSection("SettingsContainer") as SettingsContainer;