The Labs \ Source Viewer \ SSCLI \ System.Xml.XPath \ XPathNavigatorKeyComparer

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlNavigatorReader.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System.IO;
  16. using System.Xml.Schema;
  17. using System.Collections;
  18. using System.Diagnostics;
  19. using MS.Internal.Xml.Cache;
  20. namespace System.Xml.XPath
  21. {
  22.     internal class XPathNavigatorKeyComparer : IEqualityComparer
  23.     {
  24.         bool IEqualityComparer.Equals(object obj1, object obj2)
  25.         {
  26.             XPathNavigator nav1 = obj1 as XPathNavigator;
  27.             XPathNavigator nav2 = obj2 as XPathNavigator;
  28.             if ((nav1 != null) && (nav2 != null)) {
  29.                 if (nav1.IsSamePosition(nav2))
  30.                     return true;
  31.             }
  32.             return false;
  33.         }
  34.        
  35.         int IEqualityComparer.GetHashCode(object obj)
  36.         {
  37.             int hashCode;
  38.             XPathNavigator nav;
  39.             XPathDocumentNavigator xpdocNav;
  40.            
  41.             if (obj == null) {
  42.                 throw new ArgumentNullException("obj");
  43.             }
  44.             else if (null != (xpdocNav = obj as XPathDocumentNavigator)) {
  45.                 hashCode = xpdocNav.GetPositionHashCode();
  46.             }
  47.             else if (null != (nav = obj as XPathNavigator)) {
  48.                 object underlyingObject = nav.UnderlyingObject;
  49.                 if (underlyingObject != null) {
  50.                     hashCode = underlyingObject.GetHashCode();
  51.                 }
  52.                 else {
  53.                     hashCode = (int)nav.NodeType;
  54.                     hashCode ^= nav.LocalName.GetHashCode();
  55.                     hashCode ^= nav.Prefix.GetHashCode();
  56.                     hashCode ^= nav.NamespaceURI.GetHashCode();
  57.                 }
  58.             }
  59.             else {
  60.                 hashCode = obj.GetHashCode();
  61.             }
  62.             return hashCode;
  63.         }
  64.     }
  65. }

Developer Fusion