首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

孟子,思归,慕白,能不能救小弟我于水火啊,小弟我真是晕倒多次了

2011-12-14 
孟子,思归,慕白,能不能救我于水火啊,我真是晕倒多次了。原贴子是http://community.csdn.net/Expert/topic/5

孟子,思归,慕白,能不能救我于水火啊,我真是晕倒多次了。
原贴子是
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;
                        }
                }
        }

[解决办法]
配置文件里面的
[解决办法]
<add key= "MyBookShop.DataAccess.ConnectionString " value= "server=localhost; User ID=user;Password=user;database=MyBookShop " />


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还提供了允许自己定义标记的方法,以满足客户扩充的需要,这些标记是自己的定义,系统提供的方法无法读取,怎么办?因此就出现了读取自定义配置的类,你的代码就是读取自定义配置这部分用的。



[解决办法]
配置文件的文件名称和位置都是固定的。
在.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>
[解决办法]
return (string)this[ "value "];//这是这个,这个私有变量是哪 来的??没有发现在哪定义的啊???

==============

事实上,我无法理解,你说的“私有变量”指的是哪个, 这句当中,我没发现有 私有变量 的出现



假如,你无法理解的话,请尝试,跟我的思路走 ->

1.
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
{ //...
[解决办法]
.NET 类库中 ConfigurationElement ConfigurationElementCollection ConfigurationSection ConfigurationSectionGroup ... 这些类为了我们封装实现了基本的配置文件处理逻辑,我们才能如此容易扩展,
你要深刻理解,只有翻阅 MSDN,并进行一定量的尝试,
初学者并不好理解,我现在也是懵懵懂懂而已,
对于对 OO 不熟悉,就更头疼了,因为这些包含了较复杂的继承层次以及相关的接口


假如你需要,我可以发一个【更加】“自定义的”配置节处理程序给你,

与你提到的作者实现方式,不同的是,我采取了,程序编码显示的实现

声明方式 和 编程实现 是实现自定义配置节处理 的两种方式

xfwang0724@gmail.com
[解决办法]
发现 gmail 将 yahoo 的邮件全丢进垃圾箱了 .....

热点排行