原贴子是
http://community.csdn.net/Expert/topic/5711/5711395.xml?temp=.7286493
我在读以下文章时
.net2.0 自定义配置节 (处理自定义配置文件) 文件来源是:
http://www.cnblogs.com/luoxiao/archive/2007/08/03/842199.html
不明白其中一个变量如何而来的 public class MyBookShopConfigurationElement : ConfigurationElement
{
public MyBookShopConfigurationElement(String key, String value)
{
this.Key = key;
this.Value = value;
} public MyBookShopConfigurationElement()
{
} [ConfigurationProperty("key", DefaultValue = "", IsRequired = true, IsKey = true)]
public string Key
{
get
{
return (string)this["key"];
}
set
{
this["key"] = value;
}
} [ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
//[RegexStringValidator(@"\w+:\/\/[\w.]+\S*")]
public string Value
{
get
{
return (string)this["value"];//这是这个,这个私有变量是哪 来的??没有发现在哪定义的啊???
}
set
{
this["value"] = value;
}
}
}
http://community.csdn.net/Expert/topic/5711/5711395.xml?temp=.7286493
我在读以下文章时
.net2.0 自定义配置节 (处理自定义配置文件) 文件来源是:
http://www.cnblogs.com/luoxiao/archive/2007/08/03/842199.html
不明白其中一个变量如何而来的 public class MyBookShopConfigurationElement : ConfigurationElement
{
public MyBookShopConfigurationElement(String key, String value)
{
this.Key = key;
this.Value = value;
} public MyBookShopConfigurationElement()
{
} [ConfigurationProperty("key", DefaultValue = "", IsRequired = true, IsKey = true)]
public string Key
{
get
{
return (string)this["key"];
}
set
{
this["key"] = value;
}
} [ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
//[RegexStringValidator(@"\w+:\/\/[\w.]+\S*")]
public string Value
{
get
{
return (string)this["value"];//这是这个,这个私有变量是哪 来的??没有发现在哪定义的啊???
}
set
{
this["value"] = value;
}
}
}
key,value属性
http://msdn2.microsoft.com/zh-cn/library/system.configuration.configurationsection%28VS.80%29.aspx<?xml version="1.0" encoding="utf-8"?> <configuration>
<configSections> <section name="CustomSection" type="Samples.AspNet, CustomConfigurationSection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" /> </configSections> <CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />
</configuration> 要这样写
public TimeSpan MaxIdleTime
{
get
{
return (TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"] = value;
}
}
所以,里面写什么完全自己控制的
配置文件不一定就是web.config ,还有machine.config等不是任何一个文件就能拿来读取的。
可是,这叫自定义配置节,怎么还自动生成呢??真是晕倒了
Configuration 是允许进行编程访问以编辑配置文件的类。
您可以按照以下说明访问这些文件以进行读写操作。
配置文件中的标记有些是标准的,只能按照固定的方法读写
如
<appSettings>
<add key="emplate" value="~/Template.ascx" />
</appSettings>
这样的标记由系统提供的方法读写,像以前的
ConfigurationSettings.AppSettings["emplate"](.net 2.0已经废弃).net还提供了允许自己定义标记的方法,以满足客户扩充的需要,这些标记是自己的定义,系统提供的方法无法读取,怎么办?因此就出现了读取自定义配置的类,你的代码就是读取自定义配置这部分用的。
你咋么路过了呢,说说,求求你了,我都等很长时间了。
就是不明白为什么,程序代码怎么一下就控制了配置文件了呢??
并且,
public string Value
{
get
{
return (string)this["value"];//这里的索引值怎么就成了配置节的值了呢?? }
set
{
this["value"] = value;
}
}
}
在.net 1.1里,直接写
System.ConfigurationSetting.Appsettings["sqlconnstr"]
就能得到值而不用指定文件位置和名称,为什么?道理是一样的,有些东西系统已经固定了<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetting>
<add key="sqlconnstr" value="server=localhost;database=mydata;UID=sa;Password=123456" />
<add key="oledbdata" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:web\web\net\szd_book\data\db.mdb" />
</appSetting>
</configuration>
this[...]
这个知道吧, C# 对索引器属性的访问这是 C# 中特有的属性索引器访问语法, 这是实际上访问的是基类 ConfigurationElement 的属性索引器2.
类 ConfigurationElement 的索引器公开了,获取或设置此 ConfigurationElement 对象的属性 (Property)、特性 (Attribute) 或子元素 API,
而 this[...] 内部访问的是一个名为 Properties (类型为 System.Configuration.ConfigurationPropertyCollection )的集合属性ConfigurationElement 对象表示 .config 文件中一个 xml 节点,如 web.config 中的
<appSettings>
<add key="SqlConnectionString" value="server=.;database=Northwind;uid=sa;pwd=911119" />
</appSettings>其中 <appSettings> 和 <add ... /> 都是 ConfigurationElement而对于后者其 Properties 属性即表示 key="SqlConnectionString" 和 value="server=.;database=Northwind;uid=sa;pwd=911119" 这样的 键/值对 集合,3.
其中的 "value" 实际上是 作者以利用“特性”以“声明”方式定义的 -> [ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
//[RegexStringValidator(@"\w+:\/\/[\w.]+\S*")]
public string Value
{ //...
你要深刻理解,只有翻阅 MSDN,并进行一定量的尝试,
初学者并不好理解,我现在也是懵懵懂懂而已,
对于对 OO 不熟悉,就更头疼了,因为这些包含了较复杂的继承层次以及相关的接口
假如你需要,我可以发一个【更加】“自定义的”配置节处理程序给你,与你提到的作者实现方式,不同的是,我采取了,程序编码显示的实现声明方式 和 编程实现 是实现自定义配置节处理 的两种方式[email protected]
<section name="MyBookShopConfiguration" type="MyBookShop.Common.MyBookShopConfigurationSection, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections><MyBookShopConfiguration>
<BookShopConfigCollection>
<clear />
<add key="MyBookShop.DataAccess.ConnectionString" value="server=localhost; User ID=user;Password=user;database=MyBookShop" />
</BookShopConfigCollection>
</MyBookShopConfiguration>
<appSettings/>
<connectionStrings/>以此为例,当要读取或删除或添加MyBookShopConfiguration时,框架是不是自动调用 MyBookShop.Common.MyBookShopConfigurationSection来处理呢???请您明说,谢谢。
我已经给您发信了,麻烦您了。
yahoo也把您的邮件给丢进了垃圾邮箱中,并且是乱码。
================================================================================
孟子大哥,谢谢您的热心帮助,让我自己再想想,我正在体会你给的MSDN中文章的话。
晚12点之前结分。也谢谢各位看贴的朋友。
================================================================================如果你做不到...请不要轻易做出承诺....
您好,我是昨天,您给我代码那个楼主。我看了您的代码,思路清晰了许多,真是希望你把这些代码整理到你的Blog上去,供更多的初学者参考。我在这里还有几点疑问,希望您给予解答。1,SearchEnginesSection.cs中的private static readonly ConfigurationProperty _propEngine = new ConfigurationProperty("engine", typeof(SearchEngine));它被生明后,却没有再使用,去掉了,也不影响运行,我认为这个是多写上去的是不是这样呢?
2,SearchEngineCollection.cs中[ConfigurationCollection(typeof(SearchEngine), AddItemName = SearchEngineCollection.AddItemName, CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class SearchEngineCollection : ConfigurationElementCollection
{
// Consts
private const string AddItemName = "engine";[ConfigurationCollection(typeof……,此处您是用了特性,不知道是为了什么,可不可以去掉呢,我不太明白,它的实际用处。