×××标识处是我不懂的地方。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);
}这是出于什么目地呢??
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);
}这是出于什么目地呢??
//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 中对实现自定义配置节处理程序的“规则”
{
SearchEngine engine = searchEngine as SearchEngine;
if ((engine != null) && (engine.Name == this.Name))
{
return (engine.Url == this.Url);
}
return false;
}同时,重写 GetHashCode,因为基于 Equals 的实现方式,我得保证只要两个 SearhEngine 实例的 Name 和 Url 相等,此实例的 HashCode 就得相等,public override int GetHashCode()
{
int h1 = this.Name.GetHashCode();
int h2 = this.Url.GetHashCode();
return (((h1 << 5) + h1) ^ h2); // 使用一个比较简单的移位处理
}Hope helpful.