![]()
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ClassGenerator.ConfigSection
{
public class MyKeyValuePair : ConfigurationElement
{
public MyKeyValuePair() { }
public MyKeyValuePair(string key, string value)
{
this.Key = key;
this.Value = value;
}
[ConfigurationProperty("key", DefaultValue = "", IsRequired = true, IsKey = true)]
public string Key
{
get
{
return this["key"] as string;
}
set
{
this["key"] = value;
}
}
[ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
public string Value
{
get
{
return this["value"] as string;
}
set
{
this["value"] = value;
}
}
}
}
![]()
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ClassGenerator.ConfigSection
{
public class MyKeyValuePairCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new MyKeyValuePair();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as MyKeyValuePair).Key.ToUpper();
}
public void Add(MyKeyValuePair element)
{
base.BaseAdd(element);
}
public void Remove(string key)
{
base.BaseRemove(key.ToUpper());
}
public new MyKeyValuePair this[string key]
{
get
{
return base.BaseGet(key) as MyKeyValuePair;
}
}
}
}
![]()
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ClassGenerator.ConfigSection
{
public class MyKeyValuePairSection : ConfigurationSection
{
private const string connectionStringsName = "connectionStrings";
private const string mappingTypesName = "mappingTypes";
private const string templatesName = "templates";
public MyKeyValuePairSection()
{
}
[ConfigurationProperty(connectionStringsName, IsDefaultCollection = false)]
[ConfigurationCollection(typeof(MyKeyValuePairCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public MyKeyValuePairCollection Connections
{
get
{
return base[connectionStringsName] as MyKeyValuePairCollection;
}
}
[ConfigurationProperty(mappingTypesName, IsDefaultCollection = false)]
[ConfigurationCollection(typeof(MyKeyValuePairCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public MyKeyValuePairCollection MappingTypes
{
get
{
return base[mappingTypesName] as MyKeyValuePairCollection;
}
}
[ConfigurationProperty(templatesName, IsDefaultCollection = false)]
[ConfigurationCollection(typeof(MyKeyTextPairCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public MyKeyTextPairCollection Templates
{
get
{
return base[templatesName] as MyKeyTextPairCollection;
}
}
}
}
![]()
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ClassGenerator.ConfigSection
{
public class MyKeyTextPair : ConfigurationElement
{
public MyKeyTextPair() { }
public MyKeyTextPair(string key, string text)
{
this.Key = key;
this.Text = text;
}
protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
{
Key = reader.GetAttribute("key");
Text = reader.ReadElementContentAs(typeof(string), null) as string;
}
protected override bool SerializeElement(System.Xml.XmlWriter writer, bool serializeCollectionKey)
{
if (writer != null)
{
writer.WriteAttributeString("key", Key);
writer.WriteCData(Text);
}
return true;
}
[ConfigurationProperty("key", DefaultValue = "", IsRequired = true, IsKey = true)]
public string Key
{
get
{
return this["key"] as string;
}
set
{
this["key"] = value;
}
}
[ConfigurationProperty("text",IsRequired = false)]
public string Text
{
get
{
return this["text"] as string;
}
set
{
this["text"] = value;
}
}
}
}
![]()
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ClassGenerator.ConfigSection
{
public class MyKeyTextPairCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new MyKeyTextPair();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as MyKeyTextPair).Key.ToUpper();
}
public void Add(MyKeyTextPair element)
{
base.BaseAdd(element);
}
public void Remove(string key)
{
base.BaseRemove(key.ToUpper());
}
public new MyKeyTextPair this[string key]
{
get
{
return base.BaseGet(key) as MyKeyTextPair;
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySection" type="ClassGenerator.ConfigSection.MyKeyValuePairSection,ClassGenerator"/>
</configSections>
<appSettings />
<configProtectedData />
<connectionStrings />
<system.diagnostics />
<system.windows.forms />
<uri />
<MySection>
<connectionStrings>
<add key="a" value="aaaaaa" />
<add key="b" value="bbbbbb" />
</connectionStrings>
<mappingTypes>
<add key="int" value="Int32" />
<add key="char" value="String" />
<add key="varchar" value="String" />
<add key="nvarchar" value="String" />
<add key="bit" value="Boolean" />
<add key="decimal" value="Decimal" />
<add key="datetime" value="DateTime" />
<add key="bigint" value="Int64" />
</mappingTypes>
<templates>
<add key="abc">
<![CDATA[
bbb
test
test]]>
</add>
<add key="dce">
<![CDATA[
bbbtest
test]]>
</add>
<add key="aaatest">
<![CDATA[bbbtest]]>
</add>
</templates>
</MySection>