C#.Net 4.0 - 包含属性和部分的自定义配置文件

时间:2016-05-23 13:57:45

标签: c# .net web-config custom-configuration

我知道很多不同的Stackoverflow文章已经介绍过这个主题,我已经阅读了大约30篇文章,以确保我所做的与这些文章相符。它(甚至来自.Net 2.0,3.0和4.0版本的答案)

我正在尝试创建一个非常简单(至少在我的脑海中)配置文件,其中包含部分上的自定义属性,然后是部分中的可选项。所以,现在代码:

<?xml version="1.0" encoding="utf-8" ?>
<CustomSiteConfiguration>
    <Sites>
        <Site siteRoot="/Site US" name="SiteUS_en">
        </Site>
        <Site siteRoot="/Site Canada" name="SiteCanada_en">
        </Site>
        <Site siteRoot="/Partner" name="Partner_en">
            <siteSettings>
                <setting name="" value="" />
            </siteSettings>
            <JavaScriptBundles>
                <file name="" />
            </JavaScriptBundles>
            <CSSBundles>
                <file name="" />
            </CSSBundles>
        </Site>
    </Sites>
</CustomSiteConfiguration>

因此,您所看到的是网站类型的全局部分,其中包含网站类型的多个部分(CollectionElementCollections)。网站由项目上的自定义属性以及该部分内的可选项目定义。因此,siteSettings是可选的,JavaScriptBundles是可选的,CSSBundles也是可选的。

C#代码如下:

适用于网站

public class CustomGlobalSiteConfiguration : ConfigurationSection
{
    public CustomGlobalSiteConfiguration() { }

    [ConfigurationProperty("Sites")]
    [ConfigurationCollection(typeof(SitesCollection), AddItemName="Site")]
    public SitesCollection Sites
    {
        get
        {
            return (SitesCollection)base["Sites"];
        }
    }
}

适用于网站集

[ConfigurationCollection(typeof(SitesCollection), AddItemName="Site")]
public class SitesCollection : ConfigurationElementCollection
{
    // Constructor
    public SitesCollection() { }

    /*
    public CustomSiteConfiguration this[int index]
    {
        get { return (CustomSiteConfiguration)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
                BaseAdd(index, value);
        }
    }   // end of public siteSetting this [int index]
     * */

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((CustomSiteConfiguration)element).name;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new SitesCollection();
    }
}

对于网站定义

/**
 * Overarching structure of the Site Item
 **/
public class CustomSiteConfiguration : ConfigurationElement
{
    [ConfigurationProperty("siteRoot")]
    public String siteRoot
    {
        get
        {
            return (String)this["siteRoot"];
        }
        set
        {
            this["siteRoot"] = value;
        }
    }

    [ConfigurationProperty("name")]
    public String name
    {
        get
        {
            return (String)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }

    [ConfigurationProperty("siteSettings", IsRequired=false)]
    public CustomSiteSiteSettings siteSettings 
    { 
        get 
        { 
            return this["siteSettings"] as CustomSiteSiteSettings;
        }
    }

    [ConfigurationProperty("JavaScriptBundles", IsRequired = false)]
    public JavaScriptBundles javaSciptBundle
    {
        get
        {
            return this["JavaScriptBundles"] as JavaScriptBundles;
        }
    }


    [ConfigurationProperty("CSSBundles", IsRequired = false)]
    public CSSBundles cssBundle
    {
        get
        {
            return this["CSSBundles"] as CSSBundles;
        }
    }


}       // end of public class CustomSiteConfiguration : ConfigurationSection

对于SiteSettings定义

/**
 * Subsection - Site Settings
 **/ 
public class CustomSiteSiteSettings : ConfigurationElementCollection
{
    // Constructor
    public CustomSiteSiteSettings() { }

    public siteSetting this [int index]
    {
        get { return (siteSetting)BaseGet(index);  }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
                BaseAdd(index, value);
        }
    }   // end of public siteSetting this [int index]

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((siteSetting)element).name;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new CustomSiteSiteSettings();
    }

}       // end of public class CustomSiteSiteSettings : ConfigurationSection

网站设置元素

public class siteSetting : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public String name
    {
        get
        {
            return (String)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }   // end of public String name

    [ConfigurationProperty("value")]
    public String value
    {
        get
        {
            return (String)this["value"];
        }
        set
        {
            this["value"] = value;
        }
    }   // end of public String value
}       // end of public class siteSetting : ConfigurationElement

我遗漏了其他物品的空间,但其他部分看起来一样。基本上,正在发生的事情是,我正在

无法识别的属性'siteRoot'。请注意,属性名称区分大小写。

看着一切,似乎我应该没事,但是,我想我可能会做太多而且缺少一些东西。任何有关这方面的帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

我已经弄清楚我的代码出了什么问题。我将在下面提供以下信息。我使用以下文章来帮助您追踪其中的一些内容:How to implement a ConfigurationSection with a ConfigurationElementCollection

我把整个代码库都归还为零,并从头开始构建它。 XML仍然是相同的

<?xml version="1.0" encoding="utf-8" ?>
<CustomSiteConfiguration>
<Sites>
        <Site siteRoot="/Site US" name="SiteUS_en">
        </Site>
        <Site siteRoot="/Site Canada" name="SiteCanada_en">
        </Site>
        <Site siteRoot="/Partner" name="Partner_en">
            <siteSettings>
                <setting name="" value="" />
            </siteSettings>
            <JavaScriptBundles>
                <file name="" />
            </JavaScriptBundles>
            <CSSBundles>
                <file name="" />
            </CSSBundles>
        </Site>
    </Sites>
</CustomSiteConfiguration>

所以,首先我开始使用Sites Container

public class CustomSiteSettingsSection : ConfigurationSection
{
    [ConfigurationProperty("Sites")]
    [ConfigurationCollection(typeof(SiteCollection), AddItemName="Site")]
    public SiteCollection Sites
    {
        get
        {
            return (SiteCollection)base["Sites"];
        }
    }           // end of public SiteCollection Site
}               // end of public class CustomSiteSettings : ConfigurationSection { 

然后我添加了网站元素集合的SiteCollection

public class SiteCollection : ConfigurationElementCollection
{
    // Constructor
    public SiteCollection() { }

    public SiteElement this[int index]
    {
        get { return (SiteElement)BaseGet(index);  }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }       // end of public SiteElement this[int index]

    protected override ConfigurationElement CreateNewElement()
    {
        return new SiteElement();
    }       // end of protected override ConfigurationElement CreateNewElement()

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((SiteElement)element).name;
    }
}           // end of public class SiteCollection : ConfigurationElementCollection

然后我添加了具有可选值的网站定义

public class SiteElement : ConfigurationElement
{
    // Constructor
    public SiteElement() { }

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public String name
    {
        get { return (String)this["name"]; }
        set { this["name"] = value; }
    }           // end of public String name

    [ConfigurationProperty("siteRoot", IsRequired = true)]
    public String siteRoot
    {
        get { return (String)this["siteRoot"]; }
        set { this["siteRoot"] = value; }
    }           // end of public String siteRoot

    [ConfigurationProperty("siteSettings", IsRequired=false)]
    [ConfigurationCollection(typeof(SiteSettingsElementCollection), AddItemName = "setting")]
    public SiteSettingsElementCollection siteSettings
    {
        get
        {
            return (SiteSettingsElementCollection)base["siteSettings"];
        }
    }           // end of public SiteCollection Site
}               // end of public class SiteElement : ConfigurationElement

接下来我添加了SiteSettings Collection

public class SiteSettingsElementCollection : ConfigurationElementCollection
{
    // Constructor
    public SiteSettingsElementCollection() { }

    public SiteSettingElement this[int index]
    {
        get { return (SiteSettingElement)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }       // end of public SiteElement this[int index]

    protected override ConfigurationElement CreateNewElement()
    {
        return new SiteSettingElement();
    }       // end of protected override ConfigurationElement CreateNewElement()

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((SiteSettingElement)element).name;
    }
}           // end of public class SiteCollection : ConfigurationElementCollection

最后,我添加了设置元素定义

public class SiteSettingElement : ConfigurationElement
{
    public SiteSettingElement() { }

    [ConfigurationProperty("name", IsRequired=true, IsKey=true)]
    public String name
    {
        get { return (String)this["name"]; }
        set { this["name"] = value; }
    }           // end of public String name

    [ConfigurationProperty("value", IsRequired = true)]
    public String value
    {
        get { return (String)this["value"]; }
        set { this["value"] = value; }
    }           // end of public String value
}               // end of public class SiteSettingElement : ConfigurationElement

此时,我只是对两个捆绑包重复相同的操作。最后这一切都有效,并允许可选的设置和部分。