The Labs \ Source Viewer \ SSCLI \ System.Collections \ Comparer

  1. // ==++==
  2. //
  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. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  Comparer
  18. **
  19. **
  20. ** Purpose: Default IComparer implementation.
  21. **
  22. **
  23. ===========================================================*/
  24. namespace System.Collections
  25. {
  26.    
  27.     using System;
  28.     using System.Globalization;
  29.     using System.Runtime.Serialization;
  30.     using System.Security.Permissions;
  31.    
  32.     [Serializable()]
  33.     [System.Runtime.InteropServices.ComVisible(true)]
  34.     public sealed class Comparer : IComparer, ISerializable
  35.     {
  36.         private CompareInfo m_compareInfo;
  37.         public static readonly Comparer Default = new Comparer(CultureInfo.CurrentCulture);
  38.         public static readonly Comparer DefaultInvariant = new Comparer(CultureInfo.InvariantCulture);
  39.        
  40.         private const string CompareInfoName = "CompareInfo";
  41.        
  42.         private Comparer()
  43.         {
  44.             m_compareInfo = null;
  45.         }
  46.        
  47.         public Comparer(CultureInfo culture)
  48.         {
  49.             if (culture == null) {
  50.                 throw new ArgumentNullException("culture");
  51.             }
  52.             m_compareInfo = culture.CompareInfo;
  53.         }
  54.        
  55.         private Comparer(SerializationInfo info, StreamingContext context)
  56.         {
  57.             m_compareInfo = null;
  58.             SerializationInfoEnumerator enumerator = info.GetEnumerator();
  59.             while (enumerator.MoveNext()) {
  60.                 switch (enumerator.Name) {
  61.                     case CompareInfoName:
  62.                         m_compareInfo = (CompareInfo)info.GetValue(CompareInfoName, typeof(CompareInfo));
  63.                         break;
  64.                 }
  65.             }
  66.         }
  67.        
  68.         // Compares two Objects by calling CompareTo. If a ==
  69.         // b,0 is returned. If a implements
  70.         // IComparable, a.CompareTo(b) is returned. If a
  71.         // doesn't implement IComparable and b does,
  72.         // -(b.CompareTo(a)) is returned, otherwise an
  73.         // exception is thrown.
  74.         //
  75.         public int Compare(object a, object b)
  76.         {
  77.             if (a == b)
  78.                 return 0;
  79.             if (a == null)
  80.                 return -1;
  81.             if (b == null)
  82.                 return 1;
  83.             if (m_compareInfo != null) {
  84.                 string sa = a as string;
  85.                 string sb = b as string;
  86.                 if (sa != null && sb != null)
  87.                     return m_compareInfo.Compare(sa, sb);
  88.             }
  89.            
  90.             IComparable ia = a as IComparable;
  91.             if (ia != null)
  92.                 return ia.CompareTo(b);
  93.            
  94.             throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
  95.         }
  96.        
  97.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  98.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  99.         {
  100.             if (info == null) {
  101.                 throw new ArgumentNullException("info");
  102.             }
  103.            
  104.             if (m_compareInfo != null) {
  105.                 info.AddValue(CompareInfoName, m_compareInfo);
  106.             }
  107.         }
  108.     }
  109. }

Developer Fusion