C#-从ConfigurationElement属性带有参数的调用方法

时间:2018-08-28 19:15:09

标签: c# properties configurationsection

在阅读我的config部分时,如果可能的话,我想调用一个将指定值转换为ConsoleKey的方法。

我的配置部分如下:

<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>

当读取KeyBinding值时,我想尝试将其转换为ConsoleKey。在这种情况下,配置中的值为1,我想将其转换为ConsoleKey.NumPad1

我的配置元素如下:

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");
        }
    }
}

我试图将KeyBinding的值传递到GetConsoleKeyFromString中,以便返回相关的ConsoleKey

我遇到的问题是,传递给此方法的值是null。如何将我的KeyBinding值从我的app.config传递到我的GetConsoleKeyFromString中,以便将字符串值转换为ConsoleKey?

编辑

为了完整性,我已经更新了ConfigurationSection类以包含完整的代码。此外,我使用下面的代码来尝试获得此配置:

var Settings = (SettingsContainer)ConfigurationManager.GetSection("SettingsContainer") as SettingsContainer;

0 个答案:

没有答案