Explorar el Código

Removed usages of SortedList`2 for Silverlight compatibility

csharptest hace 14 años
padre
commit
8601fc98a4
Se han modificado 2 ficheros con 20 adiciones y 4 borrados
  1. 2 2
      src/ProtocolBuffers/EnumLite.cs
  2. 18 2
      src/ProtocolBuffers/FieldSet.cs

+ 2 - 2
src/ProtocolBuffers/EnumLite.cs

@@ -93,11 +93,11 @@ namespace Google.ProtocolBuffers
             }
         }
 
-        private readonly SortedList<int, IEnumLite> items;
+        private readonly Dictionary<int, IEnumLite> items;
 
         public EnumLiteMap()
         {
-            items = new SortedList<int, IEnumLite>();
+            items = new Dictionary<int, IEnumLite>();
 #if SILVERLIGHT2
     // Silverlight doesn't support Enum.GetValues
     // TODO(jonskeet): Validate that this reflection is permitted, e.g. in Windows Phone 7

+ 18 - 2
src/ProtocolBuffers/FieldSet.cs

@@ -87,8 +87,12 @@ namespace Google.ProtocolBuffers
 
         public static FieldSet CreateInstance()
         {
+#if SILVERLIGHT2
+            return new FieldSet(new Dictionary<IFieldDescriptorLite, object>());
+#else
             // Use SortedList to keep fields in the canonical order
             return new FieldSet(new SortedList<IFieldDescriptorLite, object>());
+#endif
         }
 
         /// <summary>
@@ -309,7 +313,16 @@ namespace Google.ProtocolBuffers
         /// </summary>
         internal IEnumerator<KeyValuePair<IFieldDescriptorLite, object>> GetEnumerator()
         {
+#if SILVERLIGHT2
+            List<KeyValuePair<IFieldDescriptorLite, object>> result = new List<KeyValuePair<IFieldDescriptorLite, object>>(fields);
+            result.Sort(
+                delegate(KeyValuePair<IFieldDescriptorLite, object> a, KeyValuePair<IFieldDescriptorLite, object> b)
+                { return a.Key.CompareTo(b.Key); }
+            );
+            return result.GetEnumerator();
+#else
             return fields.GetEnumerator();
+#endif
         }
 
         /// <summary>
@@ -461,9 +474,12 @@ namespace Google.ProtocolBuffers
         /// </summary>
         public void WriteTo(ICodedOutputStream output)
         {
-            foreach (KeyValuePair<IFieldDescriptorLite, object> entry in fields)
+            using (IEnumerator<KeyValuePair<IFieldDescriptorLite, object>> e = GetEnumerator())
             {
-                WriteField(entry.Key, entry.Value, output);
+                while (e.MoveNext())
+                {
+                    WriteField(e.Current.Key, e.Current.Value, output);
+                }
             }
         }