×××标识处是我不懂的地方。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);
    }这是出于什么目地呢??

解决方案 »

  1.   

    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 中对实现自定义配置节处理程序的“规则”
      

  2.   

    第二 和 第三个问题,实际上是紧密联系在一起的对于处理判断实例相等的逻辑时,假如你重写了 Equals 或者 GetHashCode ,那么你最好同时重写另一个方法,否则会导致一些非预期的结果。这个主题本身比较大,讨论起来比较麻烦 ....你可以参考 Jeffrey 的 《.NET 应用框架程序设计》相关主题这里,我重写这两个方法,主要是考虑,需要将 SearchEngine 加入集合 SearchEngineCollection ,后者是一个基于【 键/值 】对的集合我将  SearchEngine 当作【值对象】,重写 Equals 实现只要两个 SearchEngine 实例的 【Name 属性和 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;
    }同时,重写 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.