1

我创建了我的第一个自定义配置部分,一切顺利。然后我去延长它,它横着走。我遇到并且找不到很多示例的问题是,在我的部分中,我想要有 2 种不同类型的集合。两个集合中的元素完全不同,但每个集合中的元素相同。当我收到错误时,我无法让配置部分返回正确的集合。我可以作弊并创建 2 个部分,但这似乎不是实现这一点的正确方法。

好的一些代码

<configSections>
  <section name="MyFileSection" type="My.ConfigManager.MyFileListConfiguration, MyConfigManager" />
</configSections>
<MyFileSection>
  <MyDirectoryRootCollection>
    <add rootName="MyDataLocation"   rootLocation="\\MyServer\MyDirectory"/>
    <add rootName="YourDataLocation" rootLocation="\\YourServer\YourDirectory"/>
  </MyDirectoryRootCollection>
  <MyFileListCollection>
    <add keyName="MyFile1"   copyType="File" sourceFileName="TestFile1" />
    <add keyName="MyFile2"   copyType="FTP"  sourceFileName="TestFile2" />
    <add keyName="MyFile3"   copyType="File" sourceFileName="TestFile3" />
  </MyFileListCollection>
</MyFileSection>

所以在 MyFileSection 中,我有 2 个集合... MyDirectoryRootCollection 和 MyFileListCollection。我认为一个 Config 类会让我像这样得到其中的 2 个。

namespace My.ConfigManager
{
    public class MyFileListConfiguration : ConfigurationSection
    {
        private static string sMyFileListCollectionConst      = "MyFileListCollection";
        private static string sMyDirectoryRootCollectionConst = "MyDirectoryRootCollection";


        [ConfigurationProperty("MyFileListCollection", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(MyFileListConfigEleCollection), AddItemName = "MyFileListCollection")]
        public MyFileListConfigEleCollection MyFileListCollection
        {
            get { return ((MyFileListConfigEleCollection)(base["MyFileListCollection"])); }
        }

        [ConfigurationProperty("MyDirectoryRootCollection", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(MyDirectoryRootConfigEleCollection), AddItemName = "MyFileListCollection")]
        public MyFileListConfiguration MyDirectoryRoot
        {
            get { return ((MyFileListConfiguration)(base["MyDirectoryRootCollection"])); }
        }

    }

我尝试通过此调用获取配置部分

MyFileListConfiguration fileListSection = (MyFileListConfiguration)ConfigurationManager.GetSection("MyFileSection");

但我得到这个错误 -

System.Configuration.ConfigurationErrorsException 未处理
Message="配置属性 'MyDirectoryRootCollection' 可能不是从 ConfigurationSection 派生的。"

有没有人知道我做错了什么,是否有可能在一个部分中有 2 个完全不同的集合?

4

2 回答 2

3

这应该适合你。

用法:

MyFileListConfiguration myConfig = (MyFileListConfiguration)ConfigurationManager.GetSection("MyFileSection");

myConfig.Files[0] or myConfig.Files["MyFile1"] => .KeyName / .CopyType / .SourceFileName
        //  myConfig.Directories[0] or myConfig.Directories["MyDataLocation"] => .RootName / .RootLocation


public class MyFileListConfiguration : ConfigurationSection
{
    [ConfigurationProperty("MyFileListCollection", IsDefaultCollection = false)]
    public MyFileListCollection Files
    {
        get { return ((MyFileListCollection)(base["MyFileListCollection"])); }
    }

    [ConfigurationProperty("MyDirectoryRootCollection", IsDefaultCollection = false)]
    public MyDirCollection Directories
    {
        get { return ((MyDirCollection)(base["MyDirectoryRootCollection"])); }
    }
}

[ConfigurationCollection(typeof(MyFileElement))]
public class MyFileListCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MyFileElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MyFileElement)(element)).KeyName;
    }

    /// <summary>
    /// Access the collection by index
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public MyFileElement this[int index]
    {
        get { return (MyFileElement)BaseGet(index); }
    }

    /// <summary>
    /// Access the collection by key name
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public new MyFileElement this[string key]
    {
        get { return (MyFileElement)BaseGet(key); }
    }
}

[ConfigurationCollection(typeof(MyDirElement))]
public class MyDirCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MyDirElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MyDirElement)(element)).RootName;
    }

    /// <summary>
    /// Access the collection by index
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public MyDirElement this[int index]
    {
        get { return (MyDirElement)BaseGet(index); }
    }

    /// <summary>
    /// Access the collection by key name
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public new MyDirElement this[string key]
    {
        get { return (MyDirElement)BaseGet(key); }
    }
}

public class MyFileElement : ConfigurationElement
{
    [ConfigurationProperty("keyName", DefaultValue = "", IsKey = true, IsRequired = true)]
    public string KeyName
    {
        get { return ((string)(base["keyName"])); }
        set { base["keyName"] = value; }
    }

    [ConfigurationProperty("copyType", DefaultValue = "")]
    public string CopyType
    {
        get { return ((string)(base["copyType"])); }
        set { base["copyType"] = value; }
    }

    [ConfigurationProperty("sourceFileName", DefaultValue = "")]
    public string SourceFileName
    {
        get { return ((string)(base["sourceFileName"])); }
        set { base["sourceFileName"] = value; }
    }
}

public class MyDirElement : ConfigurationElement
{
    [ConfigurationProperty("rootName", DefaultValue = "", IsKey = true, IsRequired = true)]
    public string RootName
    {
        get { return ((string)(base["rootName"])); }
        set { base["rootName"] = value; }
    }

    [ConfigurationProperty("rootLocation", DefaultValue = "")]
    public string RootLocation
    {
        get { return ((string)(base["rootLocation"])); }
        set { base["rootLocation"] = value; }
    }
}
于 2012-12-05T02:35:07.820 回答
0

MyDirectoryRoot 属性的类型应该是 MyDirectoryRootConfigEleCollection 而不是 MyFileListConfiguration。请注意,添加元素名称应为“添加”以匹配您的示例http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.configurationcollectionattribute.aspx

于 2012-12-05T02:31:36.040 回答