读书人

晓锋大哥请进。解决方法

发布时间: 2012-02-01 16:58:19 作者: rapoo

晓锋大哥,请进。
×××标识处是我不懂的地方。

SearchEngine.cs


using System;
using System.Configuration;

/// <summary>
/// Summary description for SearchEngine
/// </summary>
public class SearchEngine : ConfigurationElement
{
// <configSections>
// <!-- Configuration section-handler declaration area. -->
// <sectionGroup name= "ddnTest ">
// <section name= "searchEngines " type= "SearchEnginesSection " />
// </sectionGroup>
// <!-- Other <section> and <sectionGroup> elements. -->
// </configSections>
// <ddnTest>
// <searchEngines defaultEngine= "baidu " defaultEngineUrl= "http://www.baidu.com " >
// <engine name= "baidu " url= "http://www.baidu.com " />


// Fields
private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
private static readonly ConfigurationProperty _propName = new ConfigurationProperty( "name ", typeof(string), " ", ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
private static readonly ConfigurationProperty _propUrl = new ConfigurationProperty( "url ", typeof(string), " ", ConfigurationPropertyOptions.IsRequired);

// Methods
static SearchEngine()
{
_properties.Add(_propName);
_properties.Add(_propUrl);
}

public SearchEngine()
{
}

public SearchEngine(string name, string url) : this()
{
this.Name = name;
this.Url = url;
}

public override bool Equals(object searchEngine)
{
SearchEngine engine = searchEngine as SearchEngine;
if ((engine != null) && (engine.Name == this.Name))
{
return (engine.Url == this.Url);


}
return false;
}

public override int GetHashCode()
{
int h1 = this.Name.GetHashCode();
int h2 = this.Url.GetHashCode();
return (((h1 < < 5) + h1) ^ h2);
}

// Properties
protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}

public string Name
{
get
{
return (string)base[_propName];//×××××base代表基类,可是
      //ConfigurationElement类中也没有_propName这个属性啊??
}
set
{
base[_propName] = value;
}
}

public string Url
{
get
{
return (string) base[_propUrl];
}
set
{
base[_propUrl] = value;
}
}
}


我第二个疑问,为什么要重写Equals呢??

我的第三个疑问

public override int GetHashCode()
{
int h1 = this.Name.GetHashCode();
int h2 = this.Url.GetHashCode();
return (((h1 < < 5) + h1) ^ h2);
}

这是出于什么目地呢??

[解决办法]
return (string)base[_propName];//×××××base代表基类,可是


      //ConfigurationElement类中也没有_propName这个属性啊??

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

_propName 是我在 SearchEngine 这个类定义的一个私有的静态只读字段, 见前面 ->

private static readonly ConfigurationProperty _propName = new ConfigurationProperty( "name ", typeof(string), " ", ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);

并且,我在 SearchEngine 的静态构造函数中将其加入 静态私有字段 _properties (集合类型)
static SearchEngine()
{
_properties.Add(_propName);
_properties.Add(_propUrl);
}

同时,我重写了基类的 Properties 属性

protected override ConfigurationPropertyCollection Properties
{
get
{
return _properties;
}
}

而基类 this 索引器,访问的即是 Properties 属性

你会看到,整个过程是一环紧扣一环的,

只要你深刻理解 .net 中对实现自定义配置节处理程序的“规则”

读书人网 >asp.net

热点推荐