如何实现自定义配置节?

kettle 2011-01-28 10:26:31
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="tasks" type="Tools.Task.Configuration.TaskSection"/>
</configSections>
<tasks>
<task name="ss" type="t" startAt="sat" startAfter="after"/>
<task name="bb" type="t" startAt="sst" startAfer="after"/>
</tasks>
</configuration>

配置如上所示:
我要的访问代码是:
TaskSection section = ConfigurationManager.GetSection("tasks") as TaskSection;
section.tasks["ss"]可以得到一个TaskElement对象,然后可以获取type属性。可以扩展element,如上,有startAt,startAfter等...
public class TaskElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)base["name"];
}
}

[ConfigurationProperty("type", IsRequired = true)]
public string Type
{
get
{
return (string)base["type"];
}
}
}
...全文
76 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdglynn 2011-01-28
  • 打赏
  • 举报
回复
ReflectionHelper.GetObject 这个是反射的封装,你改改就好了代码不贴了

cdglynn 2011-01-28
  • 打赏
  • 举报
回复
public class ConfigurationSectionHandlerBase<T>:IConfigurationSectionHandler where T : ConfigurationSectionHandlerBase<T>
{
protected static string configName = "ApplicationConfig";
protected static T _instance;

/// <summary>
/// 配置文件实例
/// </summary>
[XmlIgnore()]
public static T Instance
{
get
{
if (_instance == null)
{
lock (typeof(T))
{
if (_instance == null)
{
_instance = ConfigurationManager.GetSection(configName) as T;

if (_instance == null)
{
_instance = CreateInstance();
_instance.SaveConfig();
}
}
}
}

return _instance;
}
}

/// <summary>
/// 配置文件所使用的编码
/// </summary>
[XmlIgnore()]
public Encoding Encoding
{
set
{
_encoding = value;
}
get
{
return _encoding;
}
} private Encoding _encoding;

/// <summary>
/// 保存配置
/// </summary>
public void SaveConfig()
{
if (_instance == null)
{
return;
}

SerializerFactory<T> factory = new SerializerFactory<T>();
ISerializer<T> serializer = factory.GetSerializer();

string xml = serializer.Serialize(_instance);

string configFile = Application.ExecutablePath + ".config";

XmlDocument configDoc = new XmlDocument();

configDoc.Load(configFile);
XmlNode node = configDoc.DocumentElement.SelectSingleNode(configName);
if (node != null)
{
configDoc.DocumentElement.RemoveChild(node);
}

//xml = xml.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
//xml = "<Data>" + xml + "</Data>";

XmlDocument nodeDoc = new XmlDocument();
nodeDoc.LoadXml(xml);

node = nodeDoc.SelectSingleNode(configName);
node = configDoc.ImportNode(node, true);

configDoc.DocumentElement.AppendChild(node);

configDoc.Save(configFile);

}

private static T CreateInstance()
{
T instance = ReflectionHelper.GetObject(typeof(T)) as T;
return instance;
}

#region IConfigurationSectionHandler Members

public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
string outXml = section.OuterXml;
XmlSerializer<T> serializer = new XmlSerializer<T>();
T o = serializer.Deserialize(outXml);

return o;
}

#endregion
}
kettle 2011-01-28
  • 打赏
  • 举报
回复
为何不用app.config?
zjx198934 2011-01-28
  • 打赏
  • 举报
回复
用INI文件 来进行你的自定义配置!
kettle 2011-01-28
  • 打赏
  • 举报
回复
多谢两位了,知道了!
kettle 2011-01-28
  • 打赏
  • 举报
回复
详细点,貌似不能解决我的问题啊。或者把我的配置放你程序里,能读出那个来吗?我找了好多地方,发现没有好的资料。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧