X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem.Runtime.Serialization.Formatters.Binary%2FObjectWriter.cs;h=b21a617bb341c0dcd41457df68ac35331eb246be;hb=9e3370d3351358044231dd1f3df5fff3720bdcc2;hp=6b67d855d8094dbb9f032c7a785bede51eebf5ee;hpb=74e64372a55692120ea5621ad3e2c902680980bd;p=mono.git diff --git a/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs b/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs index 6b67d855d80..b21a617bb34 100644 --- a/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs +++ b/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs @@ -4,8 +4,29 @@ // Lluis Sanchez Gual (lluis@ideary.com) // // (C) 2003 Lluis Sanchez Gual - -// FIXME: Implement the missing binary elements + +// +// 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. +// using System; using System.IO; @@ -13,48 +34,191 @@ using System.Collections; using System.Runtime.Serialization; using System.Runtime.Remoting.Messaging; using System.Reflection; +using System.Globalization; namespace System.Runtime.Serialization.Formatters.Binary { + abstract class TypeMetadata + { + public TypeMetadata (Type instanceType) + { + InstanceType = instanceType; + TypeAssembly = instanceType.Assembly; + } + + public Assembly TypeAssembly; + public Type InstanceType; + + public abstract void WriteAssemblies (ObjectWriter ow, BinaryWriter writer); + public abstract void WriteTypeData (ObjectWriter ow, BinaryWriter writer); + public abstract void WriteObjectData (ObjectWriter ow, BinaryWriter writer, object data); + + public virtual bool IsCompatible (TypeMetadata other) + { + return true; + } + } + + class SerializableTypeMetadata: TypeMetadata + { + Type[] types; + string[] names; + + public SerializableTypeMetadata (Type itype, SerializationInfo info): base (itype) + { + types = new Type [info.MemberCount]; + names = new string [info.MemberCount]; + + SerializationInfoEnumerator e = info.GetEnumerator (); + + int n = 0; + while (e.MoveNext ()) + { + types[n] = e.ObjectType; + names[n] = e.Name; + n++; + } + + if (info.FullTypeName != InstanceType.FullName || info.AssemblyName != TypeAssembly.FullName) + { + TypeAssembly = Assembly.Load (info.AssemblyName); + InstanceType = TypeAssembly.GetType (info.FullTypeName); + } + } + + public override bool IsCompatible (TypeMetadata other) + { + if (!(other is SerializableTypeMetadata)) return false; + + SerializableTypeMetadata tm = (SerializableTypeMetadata)other; + if (types.Length != tm.types.Length) return false; + if (TypeAssembly != tm.TypeAssembly) return false; + if (InstanceType != tm.InstanceType) return false; + for (int n=0; n= 0; dim--) - indices[dim] = array.GetLowerBound (dim); - - bool end = false; - while (!end) - { - WriteValue (writer, elementType, array.GetValue (indices)); - - for (int dim = array.Rank-1; dim >= 0; dim--) - { - indices[dim]++; - if (indices[dim] > array.GetUpperBound (dim)) - { - if (dim > 0) { - indices[dim] = array.GetLowerBound (dim); - continue; // Increment the next dimension's index - } - end = true; // That was the last dimension. Finished. - } - break; - } - } + foreach (object item in array) + WriteValue (writer, elementType, item); } } @@ -432,16 +513,98 @@ namespace System.Runtime.Serialization.Formatters.Binary Type elementType = array.GetType().GetElementType(); WriteTypeSpec (writer, elementType); - for (int n=0; n 0) { WriteNullFiller (writer, numNulls); @@ -483,10 +646,11 @@ namespace System.Runtime.Serialization.Formatters.Binary writer.Write ((int)id); } - private void WriteValue (BinaryWriter writer, Type valueType, object val) + public void WriteValue (BinaryWriter writer, Type valueType, object val) { if (val == null) { + BinaryCommon.CheckSerializable (valueType, _surrogateSelector, _context); writer.Write ((byte) BinaryElement.NullValue); } else if (BinaryCommon.IsPrimitive(val.GetType())) @@ -533,17 +697,25 @@ namespace System.Runtime.Serialization.Formatters.Binary writer.Write (str); } - private void WriteAssembly (BinaryWriter writer, int id, Assembly assembly) + public int WriteAssembly (BinaryWriter writer, Assembly assembly) { + if (assembly == ObjectWriter.CorlibAssembly) return -1; + + bool firstTime; + int id = RegisterAssembly (assembly, out firstTime); + if (!firstTime) return id; + writer.Write ((byte) BinaryElement.Assembly); writer.Write (id); if (_assemblyFormat == FormatterAssemblyStyle.Full) writer.Write (assembly.GetName ().FullName); else writer.Write (assembly.GetName ().Name); + + return id; } - private int GetAssemblyId (Assembly assembly) + public int GetAssemblyId (Assembly assembly) { return (int)_assemblyCache[assembly]; } @@ -586,7 +758,7 @@ namespace System.Runtime.Serialization.Formatters.Binary break; case TypeCode.Decimal: - writer.Write ((decimal) value); + writer.Write (((decimal) value).ToString (CultureInfo.InvariantCulture)); break; case TypeCode.Double: @@ -663,7 +835,7 @@ namespace System.Runtime.Serialization.Formatters.Binary else if (type.IsArray && type.GetArrayRank() == 1 && BinaryCommon.IsPrimitive(type.GetElementType())) { return TypeTag.ArrayOfPrimitiveType; } - else if (type.Assembly == _corlibAssembly) { + else if (type.Assembly == CorlibAssembly) { return TypeTag.RuntimeType; } else @@ -672,6 +844,8 @@ namespace System.Runtime.Serialization.Formatters.Binary public void WriteTypeSpec (BinaryWriter writer, Type type) { + // WARNING Keep in sync with EmitWriteTypeSpec + switch (GetTypeTag (type)) { case TypeTag.PrimitiveType: