diff options
Diffstat (limited to 'mcs/class/corlib/System.Runtime.Serialization')
21 files changed, 2065 insertions, 0 deletions
diff --git a/mcs/class/corlib/System.Runtime.Serialization/ChangeLog b/mcs/class/corlib/System.Runtime.Serialization/ChangeLog new file mode 100644 index 00000000000..019307650ff --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/ChangeLog @@ -0,0 +1,137 @@ +2003-01-16 Lluis Sanchez Gual <lsg@ctv.es> + + * ObjectManager.cs: Implemented and added file + * SurrogateSelector.cs: completed implementation. + * SerializationInfo.cs: corrected a bug in GetValue method. + * ObjectIDGenerator.cs: corrected a bug. Now it does not give the same + id for two different instances that return true when calling Equal. + +2002-12-06 Duncan Mak <duncan@ximian.com> + + * Formatter.cs (WriteValueType): Remove the erroneous CLSCompliant attribute. + +2002-08-16 Gonzalo Paniagua Javier <gonzalo@ximian.com> + + * FormatterServices.cs: implemented GetUninitializedObject. + PopulateObjectMembers needs a working FieldInfo.SetValue (it's + not implemented right now). + +2002-08-16 Gonzalo Paniagua Javier <gonzalo@ximian.com> + + * FormatterServices.cs:a implemented GetSerializableMembers (). + +2002-08-16 Gonzalo Paniagua Javier <gonzalo@ximian.com> + + * FormatterServices.cs: New file with some implementation. + +2002-08-16 Dietmar Maurer <dietmar@ximian.com> + + * SerializationInfo.cs: special case for null values. + use the converter everywhere. + +2002-08-14 Dietmar Maurer <dietmar@ximian.com> + + * SerializationInfo.cs: added new function to support the runtime + +2002-07-16 Gonzalo Paniagua Javier <gonzalo@ximian.com> + + * Formatter.cs: added namespace. + +2002-06-10 Duncan Mak <duncan@ximian.com> + + * Formatter.cs: Addd to CVS. + + * FormatterConverter.cs: Added to CVS. + + * SerializationInfo.cs (AddValue): Removed extra CLSCompliant attribute. + +2002-04-12 Duncan Mak <duncan@ximian.com> + + * SerializationException.cs: Added missing constructor for serialization. + +2002-03-12 Duncan Mak <duncan@ximian.com> + + * IFormatter.cs: Fix the return type of the Serialize method. + +2002/03/07 Nick Drochak <ndrochak@gol.com> + + * StreamingContextStates.cs: Add missing value (CrossAppDomain) and + adjust All value accordingly. + +2002-03-01 Duncan Mak <duncan@ximian.com> + + * ObjectIDGenerator.cs: Implemented. + +2002-02-19 Duncan Mak <duncan@ximian.com> + + * SurrogateSelector.cs: Implemented. + + * SerializationInfoEnumerator.cs: oh, and simplified the Current + property too. + + * SerializationInfo.cs: Forgot to finish up GetEnumerator (). + +2002-02-18 Duncan Mak <duncan@ximian.com> + + * SerializationInfo.cs: Converted Type.GetType calls to the faster + typeof operator. + +2002-02-16 Duncan Mak <duncan@ximian.com> + + * SurrogateSelector.cs: Stubbed out. Gonna be working on this + tomorrow. + +2002-02-15 Duncan Mak <duncan@ximian.com> + + * SerializationEntry.cs: Added internal constructor for writing + bits in SerializationInfoEnumerator. + * SerializationInfo.cs: Completed. + * SerializationInfoEnumerator.cs: Implemented. Piggybacking on + Hashtable's GetEnumerator method. + +2002-02-13 Dan Lewis <dihlewis@yahoo.co.uk> + + * SerializationInfoEnumerator.cs: New file (stub) + +2002-02-12 Duncan Mak <duncan@ximian.com> + + * SerializationBinder.cs: Implemented. + * SerializationEntry.cs: Implemented. + * SerializationInfo.cs: Fixed the get portion of the AssemblyName + property. Implemented the FullTypename property. + +2002-01-06 David Dawkins <david@dawkins.st> + + * IFormatter.cs : New file + * ISerializationSurrogate.cs : New file + * ISurrogateSelector.cs : New file + +2002-05-01 Ravi Pratap <ravi@ximian.com> + + * SerializationInfo.cs : Insert MonoTODO attribute. + +2001-08-24 Nick Drochak <ndrochak@gol.com> + + * IDeserializationCallback.cs: New File + +Wed Nov 14 17:03:30 CET 2001 Paolo Molaro <lupus@ximian.com> + + * IFormatterConverter.cs, SerializationInfo.cs: CLSCompliant updates. + +Fri Nov 2 18:40:12 CET 2001 Paolo Molaro <lupus@ximian.com> + + * SerializationException.cs: implemented. + +2001-08-24 Nick Drochak <ndrochak@gol.com> + + * SerializationInfo.cs: Added all the public methods so that the compile would not break + +2001-07-20 Miguel de Icaza <miguel@ximian.com> + + * SerializationInfo.cs: New file. + + * IFormatterConverter.cs: New file. + + * ISerializable.cs: New file. + + diff --git a/mcs/class/corlib/System.Runtime.Serialization/Formatter.cs b/mcs/class/corlib/System.Runtime.Serialization/Formatter.cs new file mode 100755 index 00000000000..4164d5ed3b4 --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/Formatter.cs @@ -0,0 +1,104 @@ +// +// System.Runtime.Serialization.Formatter.cs +// +// Duncan Mak (duncan@ximian.com) +// +// (C) Ximian, Inc. +// + +using System; +using System.Collections; +using System.IO; + +namespace System.Runtime.Serialization +{ +[CLSCompliant (false)] +[Serializable] +public abstract class Formatter : IFormatter +{ + protected Formatter () + { + } + + protected ObjectIDGenerator m_idGenerator; + protected Queue m_objectQueue; + + public abstract SerializationBinder Binder { + get; + set; + } + + public abstract StreamingContext Context { + get; + set; + } + + public abstract ISurrogateSelector SurrogateSelector { + get; + set; + } + + public abstract object Deserialize (Stream serializationStream); + + [MonoTODO] + protected virtual object GetNext (out long objID) + { + throw new NotImplementedException (); + } + + [MonoTODO] + protected virtual long Schedule (object obj) + { + throw new NotImplementedException (); + } + + public abstract void Serialize (Stream serializationStream, object graph); + + protected abstract void WriteArray (object obj, string name, Type memberType); + + protected abstract void WriteBoolean (bool val, string name); + + protected abstract void WriteByte (byte val, string name); + + protected abstract void WriteChar (char val, string name); + + protected abstract void WriteDateTime (DateTime val, string name); + + protected abstract void WriteDecimal (Decimal val, string name); + + protected abstract void WriteDouble (double val, string name); + + protected abstract void WriteInt16 (short val, string name); + + protected abstract void WriteInt32 (int val, string name); + + protected abstract void WriteInt64 (long val, string name); + + [MonoTODO] + protected virtual void WriteMember (string memberName, object data) + { + throw new NotImplementedException (); + } + + protected abstract void WriteObjectRef (object obj, string name, Type memberType); + + + protected abstract void WriteSByte (sbyte val, string name); + + + protected abstract void WriteSingle (float val, string name); + + protected abstract void WriteTimeSpan (TimeSpan val, string name); + + [CLSCompliant (false)] + protected abstract void WriteUInt16 (ushort val, string name); + + [CLSCompliant (false)] + protected abstract void WriteUInt32 (uint val, string name); + + [CLSCompliant (false)] + protected abstract void WriteUInt64 (ulong val, string name); + + protected abstract void WriteValueType (object obj, string name, Type memberType); +} +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/FormatterConverter.cs b/mcs/class/corlib/System.Runtime.Serialization/FormatterConverter.cs new file mode 100755 index 00000000000..d478ac77d15 --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/FormatterConverter.cs @@ -0,0 +1,153 @@ +// +// System.Runtime.Serialization.Formatter.cs +// +// Duncan Mak (duncan@ximian.com) +// +// (C) Ximian, Inc. +// + +using System; +using System.Runtime.Serialization; + +namespace System.Runtime.Serialization { + public class FormatterConverter : IFormatterConverter { + + public FormatterConverter () + { + } + + public object Convert (object value, Type type) + { + return System.Convert.ChangeType (value, type); + } + + public object Convert (object value, TypeCode typeCode) + { + return System.Convert.ChangeType (value, typeCode); + } + + public bool ToBoolean (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToBoolean (value); + } + + public byte ToByte (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToByte (value); + } + + public char ToChar (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToChar (value); + } + + public DateTime ToDateTime (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToDateTime (value); + } + + public decimal ToDecimal (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToDecimal (value); + } + + public double ToDouble (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToDouble (value); + } + + public short ToInt16 (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToInt16 (value); + } + + public int ToInt32 (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToInt32 (value); + } + + public long ToInt64 (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToInt64 (value); + } + + public float ToSingle (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToSingle (value); + } + + public string ToString (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToString (value); + } + + [CLSCompliant (false)] + public sbyte ToSByte (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToSByte (value); + } + + [CLSCompliant (false)] + public ushort ToUInt16 (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToUInt16 (value); + } + + [CLSCompliant (false)] + public uint ToUInt32 (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToUInt32 (value); + } + + [CLSCompliant (false)] + public ulong ToUInt64 (object value) + { + if (value == null) + throw new ArgumentNullException ("value is null."); + + return System.Convert.ToUInt64 (value); + } + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/FormatterServices.cs b/mcs/class/corlib/System.Runtime.Serialization/FormatterServices.cs new file mode 100644 index 00000000000..6243e88d59b --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/FormatterServices.cs @@ -0,0 +1,142 @@ +// +// System.Runtime.Serialization.FormatterServices +// +// Authors: +// Gonzalo Paniagua Javier (gonzalo@ximian.com) +// +// (C) 2002 Ximian, Inc (http://www.ximian.com) +// +using System; +using System.Collections; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace System.Runtime.Serialization +{ + public sealed class FormatterServices + { + private const BindingFlags fieldFlags = BindingFlags.Public | + BindingFlags.Instance | + BindingFlags.NonPublic | + BindingFlags.DeclaredOnly; + + private FormatterServices () + { + } + + public static object [] GetObjectData (object obj, MemberInfo [] members) + { + if (obj == null) + throw new ArgumentNullException ("obj"); + + if (members == null) + throw new ArgumentNullException ("members"); + + int n = members.Length; + object [] result = new object [n]; + for (int i = 0; i < n; i++) { + MemberInfo member = members [i]; + if (member == null) + throw new ArgumentNullException (String.Format ("members[{0}]", i)); + + if (member.MemberType != MemberTypes.Field) + throw new SerializationException ( + String.Format ("members [{0}] is not a field.", i)); + + FieldInfo fi = member as FieldInfo; //FIXME: Can fi be null? + result [i] = fi.GetValue (obj); + } + + return result; + } + + public static MemberInfo [] GetSerializableMembers (Type type) + { + StreamingContext st = new StreamingContext (StreamingContextStates.All); + return GetSerializableMembers (type, st); + } + + public static MemberInfo [] GetSerializableMembers (Type type, StreamingContext context) + { + if (type == null) + throw new ArgumentNullException ("type"); + + //FIXME: context? + ArrayList fields = new ArrayList (); + Type t = type; + while (t != null) { + GetFields (t, fields); + t = t.BaseType; + } + + MemberInfo [] result = new MemberInfo [fields.Count]; + fields.CopyTo (result); + return result; + } + + private static void GetFields (Type type, ArrayList fields) + { + FieldInfo [] fs = type.GetFields (fieldFlags); + foreach (FieldInfo field in fs) + if (!(field.IsNotSerialized)) + fields.Add (field); + } + + public static Type GetTypeFromAssembly (Assembly assem, string name) + { + if (assem == null) + throw new ArgumentNullException ("assem"); + + if (name == null) + throw new ArgumentNullException ("name"); + + return assem.GetType (name); + } + + [MethodImplAttribute(MethodImplOptions.InternalCall)] + private static extern object GetUninitializedObjectInternal (Type type); + + public static object GetUninitializedObject (Type type) + { + if (type == null) + throw new ArgumentNullException ("type"); + + if (type == typeof (string)) + throw new ArgumentException ("Uninitialized Strings cannot be created."); + + return GetUninitializedObjectInternal (type); + } + + public static object PopulateObjectMembers (object obj, MemberInfo [] members, object [] data) + { + if (obj == null) + throw new ArgumentNullException ("obj"); + + if (members == null) + throw new ArgumentNullException ("members"); + + if (data == null) + throw new ArgumentNullException ("data"); + + int length = members.Length; + if (length != data.Length) + throw new ArgumentException ("different length in members and data"); + + for (int i = 0; i < length; i++) { + MemberInfo member = members [i]; + if (member == null) + throw new ArgumentNullException (String.Format ("members[{0}]", i)); + + if (member.MemberType != MemberTypes.Field) + throw new SerializationException ( + String.Format ("members [{0}] is not a field.", i)); + + FieldInfo fi = member as FieldInfo; //FIXME: can fi be null? + fi.SetValue (obj, data [i]); + } + + return obj; + } + } +} + diff --git a/mcs/class/corlib/System.Runtime.Serialization/IDeserializationCallback.cs b/mcs/class/corlib/System.Runtime.Serialization/IDeserializationCallback.cs new file mode 100644 index 00000000000..a889303f51b --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/IDeserializationCallback.cs @@ -0,0 +1,14 @@ +//
+// System.Runtime.Serialization.IDeserializationCallback.cs
+//
+// Author:
+// Nick Drochak(ndrochak@gol.com)
+//
+// (C) Nick Drochak
+//
+
+namespace System.Runtime.Serialization {
+ public interface IDeserializationCallback {
+ void OnDeserialization(object sender);
+ }
+}
\ No newline at end of file diff --git a/mcs/class/corlib/System.Runtime.Serialization/IFormatter.cs b/mcs/class/corlib/System.Runtime.Serialization/IFormatter.cs new file mode 100644 index 00000000000..214f720093e --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/IFormatter.cs @@ -0,0 +1,66 @@ +//
+// System.Runtime.Serialization.IFormatter
+//
+// Author:
+// David Dawkins (david@dawkins.st)
+//
+// (C) David Dawkins
+//
+
+using System.IO;
+
+namespace System.Runtime.Serialization {
+
+ /// <summary>
+ /// Formatting for serialized objects</summary>
+ public interface IFormatter {
+
+ //
+ // Properties
+ //
+
+ /// <summary>
+ /// Get or set the SerializationBinder used
+ /// for looking up types during deserialization</summary>
+ SerializationBinder Binder
+ {
+ get;
+ set;
+ }
+
+ /// <summary>
+ /// Get or set the StreamingContext used for serialization
+ /// and deserialization</summary>
+ StreamingContext Context
+ {
+ get;
+ set;
+ }
+
+ /// <summary>
+ /// Get or set the SurrogateSelector used by the current
+ /// formatter</summary>
+ ISurrogateSelector SurrogateSelector
+ {
+ get;
+ set;
+ }
+
+ /// <summary>
+ /// Deserialize data from the specified stream, rebuilding
+ /// the object hierarchy</summary>
+ object Deserialize(
+ Stream serializationStream
+ );
+
+ /// <summary>
+ /// Serialize the specified object to the specified stream.
+ /// Object may be the root of a graph of objects to be
+ /// serialized</summary>
+ void Serialize(
+ Stream serializationStream,
+ object graph
+ );
+ }
+
+}
diff --git a/mcs/class/corlib/System.Runtime.Serialization/IFormatterConverter.cs b/mcs/class/corlib/System.Runtime.Serialization/IFormatterConverter.cs new file mode 100644 index 00000000000..b902ec339a5 --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/IFormatterConverter.cs @@ -0,0 +1,33 @@ +// +// System.Runtime.Serialization.IFormatterConverter.cs +// +// Author: +// Miguel de Icaza (miguel@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// +// + +namespace System.Runtime.Serialization { + [CLSCompliant(false)] + public interface IFormatterConverter { + object Convert (object o, Type t); + object Convert (object o, TypeCode tc); + + bool ToBoolean (object o); + byte ToByte (object o); + char ToChar (object o); + DateTime ToDateTime (object o); + Decimal ToDecimal (object o); + double ToDouble (object o); + Int16 ToInt16 (object o); + Int32 ToInt32 (object o); + Int64 ToInt64 (object o); + sbyte ToSByte (object o); + float ToSingle (object o); + string ToString (object o); + UInt16 ToUInt16 (object o); + UInt32 ToUInt32 (object o); + UInt64 ToUInt64 (object o); + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/IObjectReference.cs b/mcs/class/corlib/System.Runtime.Serialization/IObjectReference.cs new file mode 100644 index 00000000000..59f5a0200b4 --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/IObjectReference.cs @@ -0,0 +1,16 @@ +// +// System.Runtime.Serialization.IObjectReference.cs +// +// Author: +// Miguel de Icaza (miguel@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// + +namespace System.Runtime.Serialization { + + public interface IObjectReference { + object GetRealObject (StreamingContext context); + } +} + diff --git a/mcs/class/corlib/System.Runtime.Serialization/ISerializable.cs b/mcs/class/corlib/System.Runtime.Serialization/ISerializable.cs new file mode 100644 index 00000000000..64773514eba --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/ISerializable.cs @@ -0,0 +1,15 @@ +// +// System.Runtime.Serialization.ISerializable.cs +// +// Author: +// Miguel de Icaza (miguel@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// +// + +namespace System.Runtime.Serialization { + public interface ISerializable { + void GetObjectData (SerializationInfo info, StreamingContext context); + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/ISerializationSurrogate.cs b/mcs/class/corlib/System.Runtime.Serialization/ISerializationSurrogate.cs new file mode 100644 index 00000000000..21b4ecbb7dd --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/ISerializationSurrogate.cs @@ -0,0 +1,43 @@ +//
+// System.Runtime.Serialization.ISerializationSurrogate
+//
+// Author:
+// David Dawkins (david@dawkins.st)
+//
+// (C) David Dawkins
+//
+
+namespace System.Runtime.Serialization {
+
+ /// <summary>
+ /// Interface for serialization surrogates</summary>
+ public interface ISerializationSurrogate {
+
+ /// <summary>
+ /// Get the SerializationInfo necessary to serialize
+ /// the specified object </summary>
+ /// <param name="obj">Object to be serialized</param>
+ /// <param name="info">SerializationInfo to be populated</param>
+ /// <param name="context">Destination for serialization</param>
+ void GetObjectData(
+ object obj,
+ SerializationInfo info,
+ StreamingContext context
+ );
+
+ /// <summary>
+ /// Populate an object using the specified SerializationInfo </summary>
+ /// <param name="obj">Object to be populated</param>
+ /// <param name="info">Data used for populating object</param>
+ /// <param name="context">Source for deserialization of object</param>
+ /// <param name="selector>Starting point for searching for compatible surrogates</param>
+ /// <returns>The deserialized object</returns>
+ object SetObjectData(
+ object obj,
+ SerializationInfo info,
+ StreamingContext context,
+ ISurrogateSelector selector
+ );
+ }
+
+}
diff --git a/mcs/class/corlib/System.Runtime.Serialization/ISurrogateSelector.cs b/mcs/class/corlib/System.Runtime.Serialization/ISurrogateSelector.cs new file mode 100644 index 00000000000..442b01789ed --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/ISurrogateSelector.cs @@ -0,0 +1,40 @@ +//
+// System.Runtime.Serialization.ISurrogateSelector
+//
+// Author:
+// David Dawkins (david@dawkins.st)
+//
+// (C) David Dawkins
+//
+
+namespace System.Runtime.Serialization {
+
+ /// <summary>
+ /// Creation of serialization surrogate selectors</summary>
+ public interface ISurrogateSelector {
+
+ /// <summary>
+ /// Insert specified selector into available surrogates</summary>
+ void ChainSelector( ISurrogateSelector selector );
+
+ /// <summary>
+ /// Return next surrogate in the surrogate chain</summary>
+ ISurrogateSelector GetNextSelector();
+
+ /// <summary>
+ /// Fetch the surrogate according the specified type, starting
+ /// the search from the surrogate selector for the specified
+ /// StreamingContext</summary>
+ /// <param name="type">Type of the object to be serialized</param>
+ /// <param name="context">Context for the serialization/deserialization</para,>
+ /// <param name="selector">Upon return, contains a reference to the selector where the returned surrogate was found</param>
+ /// <returns>The surrogate for the specified type and context</returns>
+ ISerializationSurrogate GetSurrogate(
+ Type type,
+ StreamingContext context,
+ out ISurrogateSelector selector
+ );
+
+ }
+
+}
diff --git a/mcs/class/corlib/System.Runtime.Serialization/ObjectIDGenerator.cs b/mcs/class/corlib/System.Runtime.Serialization/ObjectIDGenerator.cs new file mode 100755 index 00000000000..fa26c380aef --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/ObjectIDGenerator.cs @@ -0,0 +1,100 @@ +// +// System.Runtime.Serialization.ObjectIDGenerator.cs +// +// Author: Duncan Mak (duncan@ximian.com) +// Lluis Sanchez (lsg@ctv.es) +// +// (C) Ximian, Inc. +// + +using System; +using System.Collections; +using System.Runtime.Serialization; + +namespace System.Runtime.Serialization +{ + [Serializable] + public class ObjectIDGenerator + { + // Private field + Hashtable table; + long current; // this is the current ID, starts at 1 + + + // ObjectIDGenerator must generate a new id for each object instance. + // If two objects have the same state (i.e. the method Equals() returns true), + // each one should have a different id. + // Thus, the object instance cannot be directly used as key of the hashtable. + // The key is then a wrapper of the object that compares object references + // instead of object content (unless the object is inmutable, like strings). + + struct InstanceWrapper + { + object _instance; + + public InstanceWrapper (object instance) + { + _instance = instance; + } + + public override bool Equals (object other) + { + InstanceWrapper ow = (InstanceWrapper)other; + if (_instance.GetType() == typeof(string)) + return _instance.Equals(ow._instance); + else + return (_instance == ow._instance); + } + + public override int GetHashCode () + { + return _instance.GetHashCode(); + } + } + + // constructor + public ObjectIDGenerator () + : base () + { + table = new Hashtable (); + current = 1; + } + + // Methods + public virtual long GetId (object obj, out bool firstTime) + { + if (obj == null) + throw new ArgumentNullException ("The obj parameter is null."); + + InstanceWrapper iw = new InstanceWrapper(obj); + + if (table.ContainsKey (iw)) { + firstTime = false; + return (long) table [iw]; + + } else { + firstTime = true; + table.Add (iw, current); + return current ++; + } + } + + public virtual long HasId (object obj, out bool firstTime) + { + if (obj == null) + throw new ArgumentNullException ("The obj parameter is null."); + + InstanceWrapper iw = new InstanceWrapper(obj); +
+ if (table.ContainsKey (iw))
+ { + firstTime = false; + return (long) table [iw]; + + } else { + firstTime = true; + return 0L; // 0 is the null ID + } + } + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs b/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs new file mode 100644 index 00000000000..e8f42855cab --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs @@ -0,0 +1,515 @@ +// +// System.Runtime.Serialization.ObjectIDGenerator.cs +// +// Author: Lluis Sanchez Gual (lsg@ctv.es) +// +// (C) 2003 Lluis Sanchez Gual +// + +using System;
+using System.Collections;
+using System.Reflection;
+
+namespace System.Runtime.Serialization
+{
+ public class ObjectManager
+ {
+ // All objects are chained in the same order as they have been registered
+ ObjectRecord _objectRecordChain = null;
+ ObjectRecord _lastObjectRecord = null;
+
+ Hashtable _objectRecords = new Hashtable();
+ bool _finalFixup = false;
+
+ ISurrogateSelector _selector;
+ StreamingContext _context;
+ int _registeredObjectsCount = 0;
+
+ public ObjectManager(ISurrogateSelector selector, StreamingContext context)
+ {
+ _selector = selector;
+ _context = context;
+ }
+
+ public virtual void DoFixups()
+ {
+ _finalFixup = true;
+
+ try
+ {
+ if (_registeredObjectsCount < _objectRecords.Count)
+ throw new SerializationException ("There are some fixups that refer to objects that have not been registered");
+
+ // Solve al pending fixups of all objects
+
+ ObjectRecord record = _objectRecordChain;
+ while (record != null)
+ {
+ if ( record.DoFixups (true, this, true) &&
+ record.LoadData(this, _selector, _context))
+ {
+ record = record.Next;
+ }
+ else
+ {
+ // There must be an unresolved IObjectReference instance.
+ // Chain the record at the end so it is solved later
+
+ if (record.ObjectInstance is IObjectReference)
+ {
+ if (record.Status == ObjectRecordStatus.ReferenceSolvingDelayed)
+ throw new SerializationException ("The object with ID " + record.ObjectID + " could not be resolved");
+ else
+ record.Status = ObjectRecordStatus.ReferenceSolvingDelayed;
+ }
+
+ ObjectRecord next = record.Next;
+ record.Next = null;
+ _lastObjectRecord.Next = record;
+ _lastObjectRecord = record;
+ record = next;
+ }
+ }
+ }
+ finally
+ {
+ _finalFixup = false;
+ }
+ }
+
+ internal ObjectRecord GetObjectRecord (long objectID)
+ {
+ ObjectRecord rec = (ObjectRecord)_objectRecords[objectID];
+ if (rec == null)
+ {
+ if (_finalFixup) throw new SerializationException ("The object with Id " + objectID + " has not been registered");
+ rec = new ObjectRecord();
+ rec.ObjectID = objectID;
+ _objectRecords[objectID] = rec;
+ }
+ if (!rec.IsRegistered && _finalFixup) throw new SerializationException ("The object with Id " + objectID + " has not been registered");
+ return rec;
+ }
+
+ public virtual object GetObject (long objectID)
+ {
+ if (objectID <= 0) throw new ArgumentOutOfRangeException("objectID","The objectID parameter is less than or equal to zero");
+ ObjectRecord rec = (ObjectRecord)_objectRecords[objectID];
+ if (rec == null || !rec.IsRegistered) return null;
+ else return rec.ObjectInstance;
+ }
+
+ public virtual void RaiseDeserializationEvent ()
+ {
+ ICollection values = _objectRecords.Values;
+ foreach (ObjectRecord record in values)
+ {
+ IDeserializationCallback obj = record.OriginalObject as IDeserializationCallback;
+ if (obj != null) obj.OnDeserialization (this);
+ }
+ }
+
+ private void AddFixup (BaseFixupRecord record)
+ {
+ record.ObjectToBeFixed.ChainFixup (record, true);
+ record.ObjectRequired.ChainFixup (record, false);
+ }
+
+ public virtual void RecordArrayElementFixup (long arrayToBeFixed, int index, long objectRequired)
+ {
+ if (arrayToBeFixed <= 0) throw new ArgumentOutOfRangeException("arrayToBeFixed","The arrayToBeFixed parameter is less than or equal to zero");
+ if (objectRequired <= 0) throw new ArgumentOutOfRangeException("objectRequired","The objectRequired parameter is less than or equal to zero");
+ ArrayFixupRecord record = new ArrayFixupRecord(GetObjectRecord(arrayToBeFixed), index, GetObjectRecord(objectRequired));
+ AddFixup (record);
+ }
+
+ public virtual void RecordArrayElementFixup (long arrayToBeFixed, int[] indices, long objectRequired)
+ {
+ if (arrayToBeFixed <= 0) throw new ArgumentOutOfRangeException("arrayToBeFixed","The arrayToBeFixed parameter is less than or equal to zero");
+ if (objectRequired <= 0) throw new ArgumentOutOfRangeException("objectRequired","The objectRequired parameter is less than or equal to zero");
+ if (indices == null) throw new ArgumentNullException("indices");
+ MultiArrayFixupRecord record = new MultiArrayFixupRecord (GetObjectRecord(arrayToBeFixed), indices, GetObjectRecord(objectRequired));
+ AddFixup (record);
+ }
+
+ public virtual void RecordDelayedFixup (long objectToBeFixed, string memberName, long objectRequired)
+ {
+ if (objectToBeFixed <= 0) throw new ArgumentOutOfRangeException("objectToBeFixed","The objectToBeFixed parameter is less than or equal to zero");
+ if (objectRequired <= 0) throw new ArgumentOutOfRangeException("objectRequired","The objectRequired parameter is less than or equal to zero");
+ if (memberName == null) throw new ArgumentNullException("memberName");
+ DelayedFixupRecord record = new DelayedFixupRecord (GetObjectRecord(objectToBeFixed), memberName, GetObjectRecord(objectRequired));
+ AddFixup (record);
+ }
+
+ public virtual void RecordFixup (long objectToBeFixed, MemberInfo member, long objectRequired)
+ {
+ if (objectToBeFixed <= 0) throw new ArgumentOutOfRangeException("objectToBeFixed","The objectToBeFixed parameter is less than or equal to zero");
+ if (objectRequired <= 0) throw new ArgumentOutOfRangeException("objectRequired","The objectRequired parameter is less than or equal to zero");
+ if (member == null) throw new ArgumentNullException("member");
+ FixupRecord record = new FixupRecord (GetObjectRecord(objectToBeFixed), member, GetObjectRecord(objectRequired));
+ AddFixup (record);
+ }
+
+ private void RegisterObjectInternal (object obj, ObjectRecord record)
+ {
+ if (obj == null) throw new ArgumentNullException("obj");
+
+ if (record.IsRegistered)
+ {
+ if (record.OriginalObject != obj) throw new SerializationException ("An object with Id " + record.ObjectID + " has already been registered");
+ else return;
+ }
+
+ record.ObjectInstance = obj;
+ record.OriginalObject = obj;
+
+ if (obj is IObjectReference) record.Status = ObjectRecordStatus.ReferenceUnsolved;
+ else record.Status = ObjectRecordStatus.ReferenceSolved;
+
+ record.DoFixups (true, this, false);
+ record.DoFixups (false, this, false);
+ _registeredObjectsCount++;
+
+ // Adds the object to the chain of registered objects. This chain
+ // is needed to be able to to perform the final fixups in the right order
+
+ if (_objectRecordChain == null)
+ {
+ _objectRecordChain = record;
+ _lastObjectRecord = record;
+ }
+ else
+ {
+ _lastObjectRecord.Next = record;
+ _lastObjectRecord = record;
+ }
+ }
+
+
+ public virtual void RegisterObject (object obj, long objectID)
+ {
+ RegisterObjectInternal (obj, GetObjectRecord (objectID));
+ }
+
+ public void RegisterObject (object obj, long objectID, SerializationInfo info)
+ {
+ if (objectID <= 0) throw new ArgumentOutOfRangeException("objectID","The objectID parameter is less than or equal to zero");
+
+ ObjectRecord record = GetObjectRecord (objectID);
+ record.Info = info;
+ RegisterObjectInternal (obj, record);
+ }
+
+ public void RegisterObject (object obj, long objectID, SerializationInfo info, long idOfContainingObj, MemberInfo member)
+ {
+ RegisterObject (obj, objectID, info, idOfContainingObj, member, null);
+ }
+
+ public void RegisterObject( object obj, long objectID, SerializationInfo info, long idOfContainingObj, MemberInfo member, int[] arrayIndex)
+ {
+ if (objectID <= 0) throw new ArgumentOutOfRangeException("objectID","The objectID parameter is less than or equal to zero");
+
+ ObjectRecord record = GetObjectRecord (objectID);
+ record.Info = info;
+ record.IdOfContainingObj = idOfContainingObj;
+ record.Member = member;
+ record.ArrayIndex = arrayIndex;
+ RegisterObjectInternal (obj, record);
+ }
+ }
+
+
+
+ // Fixup types. There is a fixup class for each fixup type.
+
+ // BaseFixupRecord
+ // Base class for all fixups
+
+ internal abstract class BaseFixupRecord
+ {
+ public BaseFixupRecord(ObjectRecord objectToBeFixed, ObjectRecord objectRequired)
+ {
+ ObjectToBeFixed = objectToBeFixed;
+ ObjectRequired = objectRequired;
+ }
+
+ public bool DoFixup (ObjectManager manager, bool strict)
+ {
+ if (ObjectToBeFixed.IsRegistered && ObjectRequired.IsInstanceReady)
+ {
+ FixupImpl (manager);
+ return true;
+ }
+ else if (strict)
+ {
+ if (!ObjectToBeFixed.IsRegistered) throw new SerializationException ("An object with ID " + ObjectToBeFixed.ObjectID + " was included in a fixup, but it has not been registered");
+ else if (!ObjectRequired.IsRegistered) throw new SerializationException ("An object with ID " + ObjectRequired.ObjectID + " was included in a fixup, but it has not been registered");
+ else return false;
+ }
+ else
+ return false;
+ }
+
+ protected abstract void FixupImpl (ObjectManager manager);
+
+ internal protected ObjectRecord ObjectToBeFixed;
+ internal protected ObjectRecord ObjectRequired;
+
+ public BaseFixupRecord NextSameContainer;
+ public BaseFixupRecord NextSameRequired;
+ }
+
+ // ArrayFixupRecord
+ // Fixup for assigning a value to one position of an array
+
+ internal class ArrayFixupRecord : BaseFixupRecord
+ {
+ int _index;
+
+ public ArrayFixupRecord (ObjectRecord objectToBeFixed, int index, ObjectRecord objectRequired): base (objectToBeFixed, objectRequired) {
+ _index = index;
+ }
+
+ protected override void FixupImpl (ObjectManager manager) {
+ Array array = (Array)ObjectToBeFixed.ObjectInstance;
+ array.SetValue (ObjectRequired.ObjectInstance, _index);
+ }
+ }
+
+ // MultiArrayFixupRecord
+ // Fixup for assigning a value to several positions of an array
+
+ internal class MultiArrayFixupRecord : BaseFixupRecord
+ {
+ int[] _indices;
+
+ public MultiArrayFixupRecord (ObjectRecord objectToBeFixed, int[] indices, ObjectRecord objectRequired): base (objectToBeFixed, objectRequired) {
+ _indices = indices;
+ }
+
+ protected override void FixupImpl (ObjectManager manager) {
+ Array array = (Array)ObjectToBeFixed.ObjectInstance;
+ array.SetValue (ObjectRequired.ObjectInstance, _indices);
+ }
+ }
+
+ // FixupRecord
+ // Fixup for assigning a value to a member of an object
+
+ internal class FixupRecord: BaseFixupRecord
+ {
+ public MemberInfo _member;
+
+ public FixupRecord (ObjectRecord objectToBeFixed, MemberInfo member, ObjectRecord objectRequired): base (objectToBeFixed, objectRequired) {
+ _member = member;
+ }
+
+ protected override void FixupImpl (ObjectManager manager) {
+ ObjectToBeFixed.SetMemberValue (manager, _member, ObjectRequired.ObjectInstance);
+ }
+ }
+
+ // DelayedFixupRecord
+ // Fixup for assigning a value to a SerializationInfo of an object
+
+ internal class DelayedFixupRecord: BaseFixupRecord
+ {
+ public string _memberName;
+
+ public DelayedFixupRecord (ObjectRecord objectToBeFixed, string memberName, ObjectRecord objectRequired): base (objectToBeFixed, objectRequired) {
+ _memberName = memberName;
+ }
+
+ protected override void FixupImpl (ObjectManager manager) {
+ ObjectToBeFixed.SetMemberValue (manager, _memberName, ObjectRequired.ObjectInstance);
+ }
+ }
+
+ // Object Record
+
+ public enum ObjectRecordStatus: byte { Unregistered, ReferenceUnsolved, ReferenceSolvingDelayed, ReferenceSolved }
+
+ internal class ObjectRecord
+ {
+ public ObjectRecordStatus Status = ObjectRecordStatus.Unregistered;
+ public object OriginalObject;
+ public object ObjectInstance;
+ public long ObjectID;
+ public SerializationInfo Info;
+ public long IdOfContainingObj;
+ public MemberInfo Member;
+ public int[] ArrayIndex;
+ public BaseFixupRecord FixupChainAsContainer;
+ public BaseFixupRecord FixupChainAsRequired;
+ public ObjectRecord Next;
+
+ public void SetMemberValue (ObjectManager manager, MemberInfo member, object value)
+ {
+ if (member is FieldInfo)
+ ((FieldInfo)member).SetValue (ObjectInstance, value);
+ else if (member is PropertyInfo)
+ ((PropertyInfo)member).SetValue (ObjectInstance, value, null);
+ else throw new SerializationException ("Cannot perform fixup");
+
+ if (Member != null)
+ {
+ ObjectRecord containerRecord = manager.GetObjectRecord (IdOfContainingObj);
+ if (containerRecord.IsRegistered)
+ containerRecord.SetMemberValue (manager, Member, ObjectInstance);
+ }
+ }
+
+ public void SetMemberValue (ObjectManager manager, string memberName, object value)
+ {
+ if (Info == null) throw new SerializationException ("Cannot perform fixup");
+ Info.AddValue (memberName, value, value.GetType());
+ }
+
+ public bool IsInstanceReady
+ {
+ // Returns true if this object is ready to be assigned to a parent object.
+ get
+ {
+ if (!IsRegistered) return false;
+ if (IsUnsolvedObjectReference) return false;
+
+ // Embedded value objects cannot be assigned to their containers until fully completed
+ if (Member != null && (HasPendingFixups || Info != null))
+ return false;
+
+ return true;
+ }
+ }
+
+ public bool IsUnsolvedObjectReference
+ {
+ get {
+ return Status != ObjectRecordStatus.ReferenceSolved;
+ }
+ }
+
+ public bool IsRegistered
+ {
+ get {
+ return Status != ObjectRecordStatus.Unregistered;
+ }
+ }
+
+ public bool DoFixups (bool asContainer, ObjectManager manager, bool strict)
+ {
+ BaseFixupRecord prevFixup = null;
+ BaseFixupRecord fixup = asContainer ? FixupChainAsContainer : FixupChainAsRequired;
+ bool allFixed = true;
+
+ while (fixup != null)
+ {
+ if (fixup.DoFixup (manager, strict))
+ {
+ UnchainFixup (fixup, prevFixup, asContainer);
+ if (asContainer) fixup.ObjectRequired.RemoveFixup (fixup, false);
+ else fixup.ObjectToBeFixed.RemoveFixup (fixup, true);
+ }
+ else
+ {
+ prevFixup = fixup;
+ allFixed = false;
+ }
+
+ fixup = asContainer ? fixup.NextSameContainer : fixup.NextSameRequired;
+ }
+ return allFixed;
+ }
+
+ public void RemoveFixup (BaseFixupRecord fixupToRemove, bool asContainer)
+ {
+ BaseFixupRecord prevFixup = null;
+ BaseFixupRecord fixup = asContainer ? FixupChainAsContainer : FixupChainAsRequired;
+ while (fixup != null)
+ {
+ if (fixup == fixupToRemove)
+ {
+ UnchainFixup (fixup, prevFixup, asContainer);
+ return;
+ }
+ prevFixup = fixup;
+ fixup = asContainer ? fixup.NextSameContainer : fixup.NextSameRequired;
+ }
+ }
+
+ private void UnchainFixup (BaseFixupRecord fixup, BaseFixupRecord prevFixup, bool asContainer)
+ {
+ if (prevFixup == null) {
+ if (asContainer) FixupChainAsContainer = fixup.NextSameContainer;
+ else FixupChainAsRequired = fixup.NextSameRequired;
+ }
+ else {
+ if (asContainer) prevFixup.NextSameContainer = fixup.NextSameContainer;
+ else prevFixup.NextSameRequired = fixup.NextSameRequired;
+ }
+ }
+
+ public void ChainFixup (BaseFixupRecord fixup, bool asContainer)
+ {
+ if (asContainer)
+ {
+ fixup.NextSameContainer = FixupChainAsContainer;
+ FixupChainAsContainer = fixup;
+ }
+ else
+ {
+ fixup.NextSameRequired = FixupChainAsRequired;
+ FixupChainAsRequired = fixup;
+ }
+ }
+
+ public bool LoadData (ObjectManager manager, ISurrogateSelector selector, StreamingContext context)
+ {
+ if (Info != null)
+ {
+ if (ObjectInstance is ISerializable)
+ {
+ object[] pars = new object[] {Info, context};
+ ConstructorInfo con = ObjectInstance.GetType().GetConstructor (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof (SerializationInfo), typeof (StreamingContext) }, null );
+ if (con == null) throw new SerializationException ("The constructor to deserialize an object of type " + ObjectInstance.GetType().FullName + " was not found.");
+ con.Invoke (ObjectInstance, pars);
+ }
+ else
+ {
+ ISurrogateSelector foundSelector;
+ ISerializationSurrogate surrogate = selector.GetSurrogate (ObjectInstance.GetType(), context, out foundSelector);
+ if (surrogate == null) throw new SerializationException ("No surrogate selector was found for type " + ObjectInstance.GetType().FullName);
+ surrogate.SetObjectData (ObjectInstance, Info, context, foundSelector);
+ }
+
+ Info = null;
+ }
+
+ if (ObjectInstance is IObjectReference && Status != ObjectRecordStatus.ReferenceSolved)
+ {
+ try {
+ ObjectInstance = ((IObjectReference)ObjectInstance).GetRealObject(context);
+ Status = ObjectRecordStatus.ReferenceSolved;
+ }
+ catch {
+ return false;
+ }
+ }
+
+ if (Member != null)
+ {
+ // If this object is a value object embedded in another object, the parent
+ // object must be updated
+
+ ObjectRecord containerRecord = manager.GetObjectRecord (IdOfContainingObj);
+ containerRecord.SetMemberValue (manager, Member, ObjectInstance);
+ }
+
+ return true;
+ }
+
+ public bool HasPendingFixups
+ {
+ get { return FixupChainAsContainer != null; }
+ }
+ }
+}
diff --git a/mcs/class/corlib/System.Runtime.Serialization/SerializationBinder.cs b/mcs/class/corlib/System.Runtime.Serialization/SerializationBinder.cs new file mode 100644 index 00000000000..30eef514b4d --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/SerializationBinder.cs @@ -0,0 +1,22 @@ +// +// System.Runtime.Serialization.SerializationBinder.cs +// +// Author: Duncan Mak (duncan@ximian.com) +// +// (C) Ximian, Inc. +// + +namespace System.Runtime.Serialization +{ + [Serializable] + public abstract class SerializationBinder + { + // Constructor + protected SerializationBinder () + : base () + { + } + + public abstract Type BindToType (string assemblyName, string typeName); + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/SerializationEntry.cs b/mcs/class/corlib/System.Runtime.Serialization/SerializationEntry.cs new file mode 100644 index 00000000000..8221509892f --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/SerializationEntry.cs @@ -0,0 +1,40 @@ +// +// System.Runtime.Serialization.SerializationEntry.cs +// +// Author: Duncan Mak (duncan@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// + +namespace System.Runtime.Serialization +{ + public struct SerializationEntry + { + string name; + Type objectType; + object value; + + // Properties + public string Name + { + get { return name; } + } + + public Type ObjectType + { + get { return objectType; } + } + + public object Value + { + get { return value; } + } + + internal SerializationEntry (string name, Type type, object value) + { + this.name = name; + this.objectType = type; + this.value = value; + } + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/SerializationException.cs b/mcs/class/corlib/System.Runtime.Serialization/SerializationException.cs new file mode 100755 index 00000000000..e5d9196745b --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/SerializationException.cs @@ -0,0 +1,39 @@ +// +// System.Runtime.Serialization/SerializationException.cd +// +// Author: +// Paolo Molaro (lupus@ximian.com) +// +// (C) 2001 Ximian, Inc. http://www.ximian.com +// + +using System; +using System.Runtime.Serialization; + +namespace System.Runtime.Serialization { + + [Serializable] + public class SerializationException : SystemException { + // Constructors + public SerializationException () + : base ("An error occurred during (de)serialization") + { + } + + public SerializationException (string message) + : base (message) + { + } + + public SerializationException (string message, Exception inner) + : base (message, inner) + { + } + + protected SerializationException (SerializationInfo info, + StreamingContext context) + : base (info, context) + { + } + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/SerializationInfo.cs b/mcs/class/corlib/System.Runtime.Serialization/SerializationInfo.cs new file mode 100644 index 00000000000..e55bccb8b7f --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/SerializationInfo.cs @@ -0,0 +1,314 @@ +// +// System.Runtime.Serialization.SerializationInfo.cs +// +// Author: +// Miguel de Icaza (miguel@ximian.com) +// Duncan Mak (duncan@ximian.com) +// Dietmar Maurer (dietmar@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// +// + +using System; +using System.Collections; + +namespace System.Runtime.Serialization +{ + public sealed class SerializationInfo + { + Hashtable serialized = new Hashtable (); + string assemblyName; // the assembly being serialized + string fullTypeName; // the type being serialized. + + [CLSCompliant (false)] IFormatterConverter converter; + + /* used by the runtime */ + private SerializationInfo (Type type) + { + assemblyName = type.Assembly.FullName; + fullTypeName = type.FullName; + converter = new FormatterConverter (); + } + + /* used by the runtime */ + private SerializationInfo (Type type, SerializationEntry [] data) + { + int len = data.Length; + + assemblyName = type.Assembly.FullName; + fullTypeName = type.FullName; + converter = new FormatterConverter (); + + for (int i = 0; i < len; i++) + serialized.Add (data [i].Name, data [i]); + } + + // Constructor + [CLSCompliant (false)] + public SerializationInfo (Type type, IFormatterConverter converter) + { + if (type == null && converter == null) + throw new ArgumentNullException ("Null arguments."); + + this.converter = converter; + assemblyName = type.Assembly.FullName; + fullTypeName = type.FullName; + } + + // Properties + public string AssemblyName + { + get { return assemblyName; } + + set { + if (value == null) + throw new ArgumentNullException ("Argument is null."); + assemblyName = value; + } + } + + public string FullTypeName + { + get { return fullTypeName; } + + set { + if ( value == null) + throw new ArgumentNullException ("Argument is null."); + fullTypeName = value; + } + } + + public int MemberCount + { + get { return serialized.Count; } + } + + // Methods + public void AddValue (string name, object value, Type type) + { + if (serialized.ContainsKey (name)) + throw new SerializationException ("Value has been serialized already."); + + SerializationEntry values = new SerializationEntry (name, type, value); + serialized.Add (name, values); + } + + public object GetValue (string name, Type type) + { + if (name == null) + throw new ArgumentNullException ("name is null."); + if (!serialized.ContainsKey (name)) + throw new SerializationException ("No element named " + name + " could be found."); + + SerializationEntry values = (SerializationEntry) serialized [name]; + + if (values.Value != null && values.Value.GetType() != type && !values.Value.GetType().IsSubclassOf (type)) + return converter.Convert (values.Value, type); + else + return values.Value; + } + + public void SetType (Type type) + { + if (type == null) + throw new ArgumentNullException ("type is null."); + + fullTypeName = type.FullName; + assemblyName = type.Assembly.FullName; + } + + public SerializationInfoEnumerator GetEnumerator () + { + return new SerializationInfoEnumerator (serialized); + } + + public void AddValue (string name, short value) + { + AddValue (name, value, typeof (System.Int16)); + } + + [CLSCompliant(false)] + public void AddValue (string name, UInt16 value) + { + AddValue (name, value, typeof (System.UInt16)); + } + + public void AddValue (string name, int value) + { + AddValue (name, value, typeof (System.Int32)); + } + + public void AddValue (string name, byte value) + { + AddValue (name, value, typeof (System.Byte)); + } + + public void AddValue (string name, bool value) + { + AddValue (name, value, typeof (System.Boolean)); + } + + public void AddValue (string name, char value) + { + AddValue (name, value, typeof (System.Char)); + } + + [CLSCompliant(false)] + public void AddValue (string name, SByte value) + { + AddValue (name, value, typeof (System.SByte)); + } + + public void AddValue (string name, double value) + { + AddValue (name, value, typeof (System.Double)); + } + + public void AddValue (string name, Decimal value) + { + AddValue (name, value, typeof (System.Decimal)); + } + + public void AddValue (string name, DateTime value) + { + AddValue (name, value, typeof (System.DateTime)); + } + + public void AddValue (string name, float value) + { + AddValue (name, value, typeof (System.Single)); + } + + [CLSCompliant(false)] + public void AddValue (string name, UInt32 value) + { + AddValue (name, value, typeof (System.UInt32)); + } + + public void AddValue (string name, long value) + { + AddValue (name, value, typeof (System.Int64)); + } + + [CLSCompliant(false)] + public void AddValue (string name, UInt64 value) + { + AddValue (name, value, typeof (System.UInt64)); + } + + public void AddValue (string name, object value) + { + if (value == null) + AddValue (name, value, typeof (System.Object)); + else + AddValue (name, value, value.GetType ()); + } + + public bool GetBoolean (string name) + { + object value = GetValue (name, typeof (System.Boolean)); + return converter.ToBoolean (value); + } + + public byte GetByte (string name) + { + object value = GetValue (name, typeof (System.Byte)); + return converter.ToByte (value); + } + + public char GetChar (string name) + { + object value = GetValue (name, typeof (System.Char)); + return converter.ToChar (value); + } + + public DateTime GetDateTime (string name) + { + object value = GetValue (name, typeof (System.DateTime)); + return converter.ToDateTime (value); + } + + public Decimal GetDecimal (string name) + { + object value = GetValue (name, typeof (System.Decimal)); + return converter.ToDecimal (value); + } + + public double GetDouble (string name) + { + object value = GetValue (name, typeof (System.Double)); + return converter.ToDouble (value); + } + + public short GetInt16 (string name) + { + object value = GetValue (name, typeof (System.Int16)); + return converter.ToInt16 (value); + } + + public int GetInt32 (string name) + { + object value = GetValue (name, typeof (System.Int32)); + return converter.ToInt32 (value); + } + + public long GetInt64 (string name) + { + object value = GetValue (name, typeof (System.Int64)); + return converter.ToInt64 (value); + } + + [CLSCompliant(false)] + public SByte GetSByte (string name) + { + object value = GetValue (name, typeof (System.SByte)); + return converter.ToSByte (value); + } + + public float GetSingle (string name) + { + object value = GetValue (name, typeof (System.Single)); + return converter.ToSingle (value); + } + + public string GetString (string name) + { + object value = GetValue (name, typeof (System.String)); + if (value == null) return null; + return converter.ToString (value); + } + + [CLSCompliant(false)] + public UInt16 GetUInt16 (string name) + { + object value = GetValue (name, typeof (System.UInt16)); + return converter.ToUInt16 (value); + } + + [CLSCompliant(false)] + public UInt32 GetUInt32 (string name) + { + object value = GetValue (name, typeof (System.UInt32)); + return converter.ToUInt32 (value); + } + [CLSCompliant(false)] + public UInt64 GetUInt64 (string name) + { + object value = GetValue (name, typeof (System.UInt64)); + return converter.ToUInt64 (value); + } + + /* used by the runtime */ + private SerializationEntry [] get_entries () + { + SerializationEntry [] res = new SerializationEntry [this.MemberCount]; + int i = 0; + + foreach (SerializationEntry e in this) + res [i++] = e; + + return res; + } + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/SerializationInfoEnumerator.cs b/mcs/class/corlib/System.Runtime.Serialization/SerializationInfoEnumerator.cs new file mode 100644 index 00000000000..a4d50af1d70 --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/SerializationInfoEnumerator.cs @@ -0,0 +1,62 @@ +// +// System.Runtime.Serialization.SerializationInfoEnumerator.cs +// +// Author: Duncan Mak (duncan@ximian.com) +// +// (C) Ximian, Inc. +// + +using System; +using System.Collections; +using System.Runtime.Serialization; + +namespace System.Runtime.Serialization +{ + public sealed class SerializationInfoEnumerator : IEnumerator + { + IDictionaryEnumerator ide; + + // Constructor + internal SerializationInfoEnumerator (Hashtable collection) + { + ide = collection.GetEnumerator (); + } + + // Properties + public SerializationEntry Current + { + get { return (SerializationEntry) ide.Value; } + } + + object IEnumerator.Current + { + get { return ide.Value; } + } + + public string Name + { + get { return this.Current.Name; } + } + + public Type ObjectType + { + get { return this.Current.ObjectType; } + } + + public object Value + { + get { return this.Current.Value; } + } + + // Methods + public bool MoveNext () + { + return ide.MoveNext (); + } + + public void Reset () + { + ide.Reset (); + } + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/StreamingContext.cs b/mcs/class/corlib/System.Runtime.Serialization/StreamingContext.cs new file mode 100644 index 00000000000..c1b5a530fde --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/StreamingContext.cs @@ -0,0 +1,58 @@ +// +// System.Runtime.Serialization.StreamingContext.cs +// +// Author: +// Miguel de Icaza (miguel@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// + +namespace System.Runtime.Serialization { + + [Serializable] + public struct StreamingContext { + StreamingContextStates state; + object additional; + + public StreamingContext (StreamingContextStates state) + { + this.state = state; + additional = null; + } + + public StreamingContext (StreamingContextStates state, object additional) + { + this.state = state; + this.additional = additional; + } + + public object Context { + get { + return additional; + } + } + + public StreamingContextStates State { + get { + return state; + } + } + + override public bool Equals (Object o) + { + StreamingContext other; + + if (!(o is StreamingContext)) + return false; + + other = (StreamingContext) o; + + return (other.state == this.state) && (other.additional == this.additional); + } + + override public int GetHashCode () + { + return (int) state; + } + } +} diff --git a/mcs/class/corlib/System.Runtime.Serialization/StreamingContextStates.cs b/mcs/class/corlib/System.Runtime.Serialization/StreamingContextStates.cs new file mode 100644 index 00000000000..d5a3858d310 --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/StreamingContextStates.cs @@ -0,0 +1,56 @@ +// StreamingContextStates.cs +// +// This code was automatically generated from +// ECMA CLI XML Library Specification. +// Generator: libgen.xsl [1.0; (C) Sergey Chaban (serge@wildwestsoftware.com)] +// Created: Wed, 5 Sep 2001 06:45:18 UTC +// Source file: all.xml +// URL: http://devresource.hp.com/devresource/Docs/TechPapers/CSharp/all.xml +// +// (C) 2001 Ximian, Inc. http://www.ximian.com + + +namespace System.Runtime.Serialization { + + + /// <summary> + /// </summary> + [Flags] + public enum StreamingContextStates { + + /// <summary> + /// </summary> + CrossProcess = 1, + + /// <summary> + /// </summary> + CrossMachine = 2, + + /// <summary> + /// </summary> + File = 4, + + /// <summary> + /// </summary> + Persistence = 8, + + /// <summary> + /// </summary> + Remoting = 16, + + /// <summary> + /// </summary> + Other = 32, + + /// <summary> + /// </summary> + Clone = 64, + + CrossAppDomain = 128, + + /// <summary> + /// </summary> + All = 255, + } // StreamingContextStates + +} // System.Runtime.Serialization diff --git a/mcs/class/corlib/System.Runtime.Serialization/SurrogateSelector.cs b/mcs/class/corlib/System.Runtime.Serialization/SurrogateSelector.cs new file mode 100644 index 00000000000..b0f3c619fec --- /dev/null +++ b/mcs/class/corlib/System.Runtime.Serialization/SurrogateSelector.cs @@ -0,0 +1,96 @@ +// +// System.Runtime.Serialization.SurrogateSelector.cs +// +// Author: Duncan Mak (duncan@ximian.com) +// Lluis Sanchez (lsg@ctv.es) +// +// (C) Ximian, Inc. +// + +using System; +using System.Collections; + +namespace System.Runtime.Serialization +{ + public class SurrogateSelector : ISurrogateSelector + { + // Fields + Hashtable Surrogates = new Hashtable (); + ISurrogateSelector nextSelector = null; + + // Constructor + public SurrogateSelector() + : base () + { + } + + // Methods + public virtual void AddSurrogate (Type type, + StreamingContext context, ISerializationSurrogate surrogate) + { + if (type == null || surrogate == null) + throw new ArgumentNullException ("Null reference."); + + string currentKey = type.FullName + "#" + context.ToString (); + + if (Surrogates.ContainsKey (currentKey)) + throw new ArgumentException ("A surrogate for " + type.FullName + " already exists."); + + Surrogates.Add (currentKey, surrogate); + } + + public virtual void ChainSelector (ISurrogateSelector selector) + { + if (selector == null) + throw new ArgumentNullException ("Selector is null."); + + // Chain the selector at the beggining of the chain + // since "The last selector added to the list will be the first one checked" + // (from MS docs) + + if (nextSelector != null) + selector.ChainSelector (nextSelector); + + nextSelector = selector; + } + + public virtual ISurrogateSelector GetNextSelector () + { + return nextSelector; + } + + public virtual ISerializationSurrogate GetSurrogate (Type type, + StreamingContext context, out ISurrogateSelector selector) + { + if (type == null) + throw new ArgumentNullException ("type is null."); + + // Check this selector, and if the surrogate is not found, + // check the chained selectors + + string key = type.FullName + "#" + context.ToString (); + ISerializationSurrogate surrogate = (ISerializationSurrogate) Surrogates [key]; + + if (surrogate != null) { + selector = this; + return surrogate; + } + + if (nextSelector != null) + return nextSelector.GetSurrogate (type, context, out selector); + else { + selector = null; + return null; + } + } + + public virtual void RemoveSurrogate (Type type, StreamingContext context) + { + if (type == null) + throw new ArgumentNullException ("type is null."); + + string key = type.FullName + "#" + context.ToString (); + Surrogates.Remove (key); + } + } +} |