如何使用和开发自定义配置节
public string CustomAttribute {
get { return (string)base["customAttribute"]; }
set { base["customAttribute"] = value; }
}
[ConfigurationProperty("customElement", DefaultValue = "", IsRequired = true)]
public CustomElementConfiguration CustomElement {
get { return (CustomElementConfiguration)base["customElement"]; }
set { base["customElement"] = value; }
}
}
public class CustomElementConfiguration : ConfigurationElement
{
public CustomElementConfiguration() { }
public CustomElementConfiguration(string value1, string value2)
{
Value1 = value1;
Value2 = value2;
}
[ConfigurationProperty("value1", DefaultValue = "", IsRequired = true)]
public string Value1 {
get { return (string)base["value1"]; }
set { base["value1"] = value; }
}
[ConfigurationProperty("value2", DefaultValue = "", IsRequired = true)]
public string Value2
{
get { return (string)base["value2"]; }
set { base["value2"] = value; }
}
}
}
第二步:在*.config中设置自定的元素
- <configuration>
-
- <configSections>
- <sectionGroup name="customGroup">
- <section
- name="customSection"
- type="
- MyCustomConfiguration.CustomSectionConfiguration, MyCustomConfiguration, Version=1.0.0.0, Culture=neutral,
- PublicKeyToken=null" allowLocation="true" allowDefinition="everywhere" />
- </sectionGroup>
- ……
-