的ConfigurationProperty(typeof运算(CCfgElement)的ToString())

时间:2014-02-07 09:03:00

标签: c# .net

为什么不可能有这个?

[ConfigurationProperty(typeof(CCfgElement).ToString())]

我收到错误:

  

错误1属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

这是我的代码

public class CConfigElement<T> : ConfigurationElement
    {
    }

    public class CConfigSection<CCfgElement> : ConfigurationSection where CCfgElement : CConfigElement<CCfgElement>
    {
        // Create a element.
        [ConfigurationProperty(typeof(CCfgElement).ToString())]
        public CCfgElement Element
        {
            get { return (CCfgElement)this[typeof(CCfgElement).ToString()]; }
            set { this[typeof(CCfgElement).ToString()] = value; }
        }
    }

我认为没有任何变通方法 任何人都可以告诉我如何添加带有代码的节段文件吗?

索伦

我找到了一种自我解决方案

这是我的代码

// create the DataModel in your MainWindow or App
//  CTestDataModel t = new CTestDataModel();

public class CTestDataModel
{
    CTestValues _Cfg;

    // Create values to save in cofig
    public class CTestValues : ConfigurationElement
    {
        [ConfigurationProperty("Param", DefaultValue = 11, IsRequired = true)]
        public int Param
        {
            get { return (int)this["Param"]; }
            set { this["Param"] = value; }
        }

        [ConfigurationProperty("Param1", DefaultValue = "22", IsRequired = true)]
        public double Param1
        {
            get { return (double)this["Param1"]; }
            set { this["Param1"] = value; }
        }

        [ConfigurationProperty("Time", IsRequired = true)]
        public DateTime Time
        {
            get { return (DateTime)this["Time"]; }
            set { this["Time"] = value; }
        }
    }

    public CTestDataModel()
    {
        // load config
        _Cfg = CConfiguration.Element<CTestValues>();

        // Use values
        int t = _Cfg.Param;
        _Cfg.Param = 5;
        _Cfg.Param1 = 6;
        _Cfg.Time = new DateTime(1962, 10, 10);
    }
}

public class CConfigSection<CCfgElement> : ConfigurationSection where CCfgElement : ConfigurationElement
{
    // Create a element.
    [ConfigurationProperty("Element")]
    public CCfgElement Element
    {
        get { return (CCfgElement)this["Element"]; }
        set { this["Element"] = value; }
    }
}

public static class CConfiguration
{
    static Configuration config = null;
    static AppSettingsSection appSettings = null;

    static CConfiguration()
    {
        config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        appSettings = config.AppSettings;

        AppDomain.CurrentDomain.ProcessExit += CConfiguration_Dtor;
    }

    private static void CConfiguration_Dtor(object sender, EventArgs e)
    {
        config.Save(ConfigurationSaveMode.Modified, true);
    }

    // Uses sections
    private static String MakeSectionName(String name)
    {
        return name.Replace('+', '.');
    }

    public static void RemoveSection(Type T)
    {
        try
        {
            String name = MakeSectionName(T.ToString());
            config.Sections.Remove(name);
        }
        catch (Exception)
        {
            return;
        }
    }

    public static T Element<T>() where T : ConfigurationElement
    {
        try
        {
            return Section<T>().Element;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(typeof(T).ToString()+" error: "+ex.Message);
            throw ex;
        }
    }

    public static CConfigSection<T> Section<T>() where T : ConfigurationElement
    {
        try
        {
            return Section(typeof(T).ToString(), new CConfigSection<T>()) as CConfigSection<T>;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(typeof(T).ToString()+" error: "+ex.Message);
            throw ex;
        }
    }

    public static ConfigurationSection Section(String name, ConfigurationSection section)
    {
        try
        {
            name = MakeSectionName(name);
            ConfigurationSection cs = config.Sections.Get(name);
            if (cs == null)
            {
                config.Sections.Add(name, section);
                return section;
            }
            else
                return cs;
        }
        catch (Exception)
        {
            config.Sections.Remove(name);
            config.Sections.Add(name, section);
            return section;
        }
    }

    // Uses AppSettings
    public static bool Exist(String key)
    {
        return appSettings.Settings[key] != null;
    }

    public static string Read(String key, string Default = "")
    {
        if (Exist(key))
            return appSettings.Settings[key].Value;
        return Default;
    }

    public static void Write(string key, string value)
    {
        if (Exist(key))
            appSettings.Settings.Remove(key);
        appSettings.Settings.Add(key, value);
    }

    public static void Remove(string key)
    {
        if (Exist(key))
            appSettings.Settings.Remove(key);
    }

    public static new string ToString()
    {
        StringBuilder sb = new StringBuilder();
        // Get the settings collection (key/value pairs).
        if (appSettings.Settings.Count != 0)
        {
            foreach (string key in appSettings.Settings.AllKeys)
            {
                string value = appSettings.Settings[key].Value;
                sb.Append(String.Format("Key: {0} Value: {1}\r\n", key, value));
            }
        }
        return sb.ToString();
    }
}

0 个答案:

没有答案