New test.
[mono.git] / mcs / class / corlib / System.Collections / Comparer.cs
index c05bc925fe6c5aaad8dd6c72e66140a0a7a0f0a3..2f2ca54a58586b8f6c42ac3d57184f35b214e591 100644 (file)
@@ -1,21 +1,45 @@
 //
 // System.Collections.Comparer.cs
 //
-// Author:
-//   Sergey Chaban (serge@wildwestsoftware.com)
+// Authors:
+//     Sergey Chaban (serge@wildwestsoftware.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
+//
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System.Globalization;
+using System.Runtime.InteropServices;
 using System.Runtime.Serialization;
+using System.Security.Permissions;
 
 namespace System.Collections
 {
-       [Serializable]
-       public sealed class Comparer : IComparer
-#if NET_1_1
-, ISerializable
+#if NET_2_0
+       [ComVisible(true)]
 #endif
-       {
+       [Serializable]
+       public sealed class Comparer : IComparer, ISerializable {
+
                public static readonly Comparer Default = new Comparer ();
 #if NET_1_1
                public
@@ -24,7 +48,8 @@ namespace System.Collections
 #endif
                static readonly Comparer DefaultInvariant = new Comparer (CultureInfo.InvariantCulture);
 
-               private CompareInfo _compareInfo;
+               // This field was introduced for MS kompatibility. see bug #77701
+               CompareInfo m_compareInfo;
 
                private Comparer ()
                {
@@ -41,7 +66,7 @@ namespace System.Collections
                        if (culture == null)
                                throw new ArgumentNullException ("culture");
 
-                       _compareInfo = culture.CompareInfo;
+                       m_compareInfo = culture.CompareInfo;
                }
 
 
@@ -55,11 +80,11 @@ namespace System.Collections
                        else if (b == null)
                                return 1;
 
-                       if (_compareInfo != null) {
+                       if (m_compareInfo != null) {
                                string sa = a as string;
                                string sb = b as string;
                                if (sa != null && sb != null)
-                                       return _compareInfo.Compare (sa, sb);
+                                       return m_compareInfo.Compare (sa, sb);
                        }
 
                        if (a is IComparable)
@@ -67,39 +92,17 @@ namespace System.Collections
                        else if (b is IComparable)
                                return -(b as IComparable).CompareTo (a);
 
-                       throw new ArgumentException (Locale.GetText ("Neither a nor b Comparable."));
-               }
-
-               #region Implementation of ISerializable
-
-#if NET_1_1
-               private Comparer (SerializationInfo info, StreamingContext context)
-               {
-                       _compareInfo = null;
-                       foreach (SerializationEntry entry in info)
-                       {
-                               if (entry.Name == "CompareInfo")
-                               {
-                                       _compareInfo = (CompareInfo) info.GetValue("CompareInfo", typeof(CompareInfo));
-                                       break;
-                               }
-                       }
+                       throw new ArgumentException (Locale.GetText ("Neither 'a' nor 'b' implements IComparable."));
                }
 
-               public void GetObjectData(SerializationInfo info, StreamingContext context)
+               // ISerializable
+               [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
+               public void GetObjectData (SerializationInfo info, StreamingContext context)
                {
-                       if (info == null)
-                       {
-                               throw new ArgumentNullException ("info");
-                       }
+                       if (info == null)
+                               throw new ArgumentNullException ("info");
 
-                       if (_compareInfo != null)
-                       {
-                               info.AddValue("CompareInfo", _compareInfo);
-                       }
+                       info.AddValue ("CompareInfo", m_compareInfo, typeof (CompareInfo));
                }
-#endif
-
-               #endregion Implementation of ISerializable
        }
 }