This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Runtime.Serialization.Formatters.Binary / ObjectWriter.cs
index da322fe28b92dc30986fd38f438ab3824f253740..11f382914cc9a9b64365dce8e9c363e6210f92d9 100644 (file)
@@ -4,8 +4,29 @@
 //   Lluis Sanchez Gual (lluis@ideary.com)\r
 //\r
 // (C) 2003 Lluis Sanchez Gual\r
-\r
-// FIXME: Implement the missing binary elements\r
+
+//
+// Copyright (C) 2004 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.
+//
 \r
 using System;\r
 using System.IO;\r
@@ -13,6 +34,7 @@ using System.Collections;
 using System.Runtime.Serialization;\r
 using System.Runtime.Remoting.Messaging;\r
 using System.Reflection;\r
+using System.Globalization;\r
 \r
 namespace System.Runtime.Serialization.Formatters.Binary\r
 {\r
@@ -145,8 +167,12 @@ namespace System.Runtime.Serialization.Formatters.Binary
                        writer.Write (members.Length);\r
 \r
                        // Names of fields\r
-                       foreach (FieldInfo field in members)\r
-                               writer.Write (field.Name);\r
+                       foreach (FieldInfo field in members) {\r
+                               if (field.DeclaringType == InstanceType)\r
+                                       writer.Write (field.Name);\r
+                               else\r
+                                       writer.Write (field.DeclaringType.Name + "+" + field.Name);\r
+                       }\r
 \r
                        // Types of fields\r
                        foreach (FieldInfo field in members)\r
@@ -356,35 +382,35 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                        return;\r
                                }\r
                                \r
+                               Hashtable typesTable;\r
+                               bool isNew = false;\r
+                               lock (_cachedTypes) {\r
+                                       typesTable = (Hashtable) _cachedTypes [_context.State];\r
+                                       if (typesTable == null) {\r
+                                               typesTable = new Hashtable ();\r
+                                               _cachedTypes [_context.State] = typesTable;\r
+                                               isNew = true;\r
+                                       }\r
+                               }\r
+\r
                                metadata = null;\r
-                               lock (_cachedTypes)\r
-                               {\r
-                                       Hashtable typesTable = (Hashtable) _cachedTypes [_context.State];\r
-                                       if (typesTable != null)\r
-                                       {\r
+                               lock (typesTable) {\r
+                                       if (!isNew) {\r
                                                metadata = (TypeMetadata) typesTable [instanceType];\r
-                                               if (metadata == null) \r
-                                               {\r
-                                                       metadata = CreateMemberTypeMetadata (instanceType);\r
-                                                       typesTable [instanceType] = metadata;\r
-                                               }\r
                                        }\r
-                                       else\r
-                                       {\r
+\r
+                                       if (metadata == null) {\r
                                                metadata = CreateMemberTypeMetadata (instanceType);\r
-                                               typesTable = new Hashtable ();\r
-                                               typesTable [instanceType] = metadata;\r
-                                               _cachedTypes [_context.State] = typesTable;\r
                                        }\r
+\r
+                                       typesTable [instanceType] = metadata;\r
                                }\r
                        }\r
                }\r
                \r
                TypeMetadata CreateMemberTypeMetadata (Type type)\r
                {\r
-                       // This environment variable is only for test and benchmarking pourposes.\r
-                       // By default, mono will always use IL generated class serializers.\r
-                       if (Environment.GetEnvironmentVariable("MONO_REFLECTION_SERIALIZER") == null) {\r
+                       if (!BinaryCommon.UseReflectionSerialization) {\r
                                Type metaType = CodeGenerator.GenerateMetadataType (type, _context);\r
                                return (TypeMetadata) Activator.CreateInstance (metaType);\r
                        }\r
@@ -460,31 +486,8 @@ namespace System.Runtime.Serialization.Formatters.Binary
                        }\r
                        else\r
                        {\r
-                               int[] indices = new int[array.Rank];\r
-\r
-                               // Initialize indexes\r
-                               for (int dim = array.Rank-1; dim >= 0; dim--)\r
-                                       indices[dim] = array.GetLowerBound (dim);\r
-\r
-                               bool end = false;\r
-                               while (!end)\r
-                               {\r
-                                       WriteValue (writer, elementType, array.GetValue (indices));\r
-\r
-                                       for (int dim = array.Rank-1; dim >= 0; dim--)\r
-                                       {\r
-                                               indices[dim]++;\r
-                                               if (indices[dim] > array.GetUpperBound (dim))\r
-                                               {\r
-                                                       if (dim > 0) {\r
-                                                               indices[dim] = array.GetLowerBound (dim);\r
-                                                               continue;       // Increment the next dimension's index\r
-                                                       }\r
-                                                       end = true;     // That was the last dimension. Finished.\r
-                                               }\r
-                                               break;\r
-                                       }\r
-                               }\r
+                               foreach (object item in array)\r
+                                       WriteValue (writer, elementType, item);\r
                        }\r
                }\r
 \r
@@ -603,9 +606,8 @@ namespace System.Runtime.Serialization.Formatters.Binary
                private void WriteSingleDimensionArrayElements (BinaryWriter writer, Array array, Type elementType)\r
                {\r
                        int numNulls = 0;\r
-                       for (int n = array.GetLowerBound (0); n<=array.GetUpperBound(0); n++)\r
+                       foreach (object val in array)\r
                        {\r
-                               object val = array.GetValue (n);\r
                                if (val != null && numNulls > 0)\r
                                {\r
                                        WriteNullFiller (writer, numNulls);\r
@@ -758,7 +760,7 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                        break;\r
 \r
                                case TypeCode.Decimal:\r
-                                       writer.Write ((decimal) value);\r
+                                       writer.Write (((decimal) value).ToString (CultureInfo.InvariantCulture));\r
                                        break;\r
 \r
                                case TypeCode.Double:\r