Browse Source

2002-02-15 Duncan Mak <[email protected]>

	    * SerializationEntry.cs: Added internal constructor bits for
	      SerializationInfoEnumerator.
	    * SerializationInfo.cs: Completed.
	    * SerializationInfoEnumerator.cs: Implemented. Piggybacking on
	      Hashtable's GetEnumerator method.

svn path=/trunk/mcs/; revision=2436
Duncan Mak 24 years ago
parent
commit
832e3630e9

+ 8 - 0
mcs/class/corlib/System.Runtime.Serialization/ChangeLog

@@ -1,3 +1,11 @@
+2002-02-15  Duncan Mak  <[email protected]>
+
+	* SerializationEntry.cs: Added internal constructor for writing
+	bits for SerializationInfoEnumerator.
+	* SerializationInfo.cs: Completed.
+	* SerializationInfoEnumerator.cs: Implemented. Piggybacking on
+	Hashtable's GetEnumerator method.
+
 2002-02-13  Dan Lewis <[email protected]>
 
 	* SerializationInfoEnumerator.cs: New file (stub)

+ 7 - 0
mcs/class/corlib/System.Runtime.Serialization/SerializationEntry.cs

@@ -29,5 +29,12 @@ namespace System.Runtime.Serialization
 		{
 			get { return value; }
 		}
+
+		internal SerializationEntry (string name, Type type, object value)
+		{
+			this.name = name;
+			this.objectType = type;
+			this.value = value;
+		}
 	}
 }

+ 230 - 93
mcs/class/corlib/System.Runtime.Serialization/SerializationInfo.cs

@@ -3,123 +3,260 @@
 //
 // Author:
 //   Miguel de Icaza ([email protected])
+//   Duncan Mak ([email protected])
 //
 // (C) Ximian, Inc.  http://www.ximian.com
 //
-// TODO: This is just a skeleton to get corlib to compile.
 //
 
-namespace System.Runtime.Serialization {
+using System;
+using System.Collections;
 
-	[MonoTODO]
-	public sealed class SerializationInfo {
-		Type type;
-		[CLSCompliant(false)] IFormatterConverter converter;
+namespace System.Runtime.Serialization
+{
+	public sealed class SerializationInfo
+	{
 		
-		[CLSCompliant(false)]
+		Hashtable serialized = new Hashtable ();
+		string assemblyName; // name of the assembly where the type/class being serialized resides.
+		string fullTypeName; // name of the type being serialized.
+
+		[CLSCompliant (false)] IFormatterConverter converter;
+
+		// Constructor
+		[CLSCompliant (false)]
 		public SerializationInfo (Type type, IFormatterConverter converter)
 		{
-			this.type = type;
+			if (type == null && converter == null)
+				throw new ArgumentNullException ("Null arguments.");
+						
 			this.converter = converter;
+			assemblyName = type.Assembly.FullName;
+			fullTypeName = type.FullName;
 		}
 
-		[MonoTODO]
-		public string AssemblyName {
-			get {
-				return type.Assembly.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 type.FullName;
-			}
+		public string FullTypeName
+		{
+			get { return fullTypeName; }
 			
 			set {
-				type = Type.GetType (value);
+				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.ObjectType != type)
+				throw new InvalidCastException ("Invalid Type casting.");
+			
+			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 null;
+		}
 		
-		public int MemberCount {
-			get {
-				return 0;
-			}
+		[CLSCompliant(false)]
+		public void AddValue (string name, short value)
+		{
+			AddValue (name, value, Type.GetType ("System.Int16"));
+		}
+
+		[CLSCompliant(false)]
+		public void AddValue (string name, UInt16 value)
+		{
+			AddValue (name, value, Type.GetType ("System.UInt16"));
+		}
+		
+		public void AddValue (string name, int value)
+		{
+			AddValue (name, value, Type.GetType ("System.Int32"));
+		}
+		
+		public void AddValue (string name, byte value)
+		{
+			AddValue (name, value, Type.GetType ("System.Byte"));
+		}
+		
+		public void AddValue (string name, bool value)
+		{
+			AddValue (name, value, Type.GetType ("System.Boolean"));
+		}
+	       
+	        public void AddValue (string name, char value)
+		{
+			AddValue (name, value, Type.GetType ("System.Char"));
 		}
 
-		//Public Instance Methods
-#region TODO: Implement these
-		[CLSCompliant(false)][MonoTODO]
-		public void AddValue(string name, short value){}
-		[CLSCompliant(false)][MonoTODO]
-		public void AddValue(string name, UInt16 value){}
-		[MonoTODO]	
-		public void AddValue(string name, int value){}
-		[MonoTODO]	
-		public void AddValue(string name, byte value){}
-		[MonoTODO]
-		public void AddValue(string name, bool value){}
-		[MonoTODO]
-	        public void AddValue(string name, char value){}
-		[CLSCompliant(false)][MonoTODO]
-	        public void AddValue(string name, SByte value){}
-		[MonoTODO]	
-	        public void AddValue(string name, double value){}
-		[MonoTODO]
-	        public void AddValue(string name, Decimal value){}
-		[MonoTODO]
-	        public void AddValue(string name, DateTime value){}
-		[MonoTODO]
-	        public void AddValue(string name, float value){}
-		[CLSCompliant(false)][MonoTODO]
-	        public void AddValue(string name, UInt32 value){}
-		[MonoTODO]
-	        public void AddValue(string name, long value){}
-		[CLSCompliant(false)][MonoTODO]
-	        public void AddValue(string name, UInt64 value){}
-		[MonoTODO]
-	        public void AddValue(string name, object value){}
-		[MonoTODO]
-		public void AddValue(string name, object value, Type type){}
-		[MonoTODO]
-		public bool GetBoolean(string name){return false;}
-		[MonoTODO]
-	        public byte GetByte(string name){return 0;}
-		[MonoTODO]
-	        public char GetChar(string name){return 'x';}
-		[MonoTODO]
-	        public DateTime GetDateTime(string name){return new DateTime();}
-		[MonoTODO]
-		public Decimal GetDecimal(string name){return new Decimal();}
-		[MonoTODO]
-		public double GetDouble(string name){return 0;}
-		[MonoTODO]
-		public System.Runtime.Serialization.SerializationInfoEnumerator GetEnumerator(){return null;}
-		[MonoTODO]
-		public short GetInt16(string name){return 0;}
-		[MonoTODO]
-		public int GetInt32(string name){return 0;}
-		[MonoTODO]
-		public long GetInt64(string name){return 0;}
-		[CLSCompliant(false)][MonoTODO]
-		public SByte GetSByte(string name){return new SByte();}
-		[MonoTODO]
-		public float GetSingle(string name){return 0;}
-		[MonoTODO]
-		public string GetString(string name){return "";}
-		[CLSCompliant(false)][MonoTODO]
-		public UInt16 GetUInt16(string name){return 0;}
-		[CLSCompliant(false)][MonoTODO]
-		public UInt32 GetUInt32(string name){return 0;}
-		[CLSCompliant(false)][MonoTODO]
-		public UInt64 GetUInt64(string name){return 0;}
-		[MonoTODO]
-		public object GetValue(string name, Type type){return null;}
-		[MonoTODO]
-		public void SetType(Type type){}
-#endregion TODO
+		[CLSCompliant(false)]
+	        public void AddValue (string name, SByte value)
+		{
+			AddValue (name, value, Type.GetType ("System.SByte"));
+		}
+		
+	        public void AddValue (string name, double value)
+		{
+			AddValue (name, value, Type.GetType ("System.Double"));
+		}
+		
+	        public void AddValue (string name, Decimal value)
+		{
+			AddValue (name, value, Type.GetType ("System.Decimal"));
+		}
+		
+	        public void AddValue (string name, DateTime value)
+		{
+			AddValue (name, value, Type.GetType ("System.DateTime"));
+		}
+		
+	        public void AddValue (string name, float value)
+		{
+			AddValue (name, value, Type.GetType ("System.Single"));
+		}
+
+		[CLSCompliant(false)]
+	        public void AddValue (string name, UInt32 value)
+		{
+			AddValue (name, value, Type.GetType ("System.UInt32"));
+		}
+	       
+	        public void AddValue (string name, long value)
+		{
+			AddValue (name, value, Type.GetType ("System.Int64"));
+		}
+		[CLSCompliant(false)]
+	        public void AddValue (string name, UInt64 value)
+		{
+			AddValue (name, value, Type.GetType ("System.UInt64"));
+		}
+		
+	        public void AddValue (string name, object value)
+		{
+			AddValue (name, value, value.GetType ());
+		}		
+		
+		public bool GetBoolean (string name)
+		{
+			return (bool) GetValue (name, Type.GetType ("System.Boolean")); 
+		}
+		
+	        public byte GetByte (string name)
+		{
+			return (byte) GetValue (name, Type.GetType ("System.Byte"));
+		}
+		
+	        public char GetChar (string name)
+		{
+			return (char) GetValue (name, Type.GetType ("System.Char"));
+		}
+
+	        public DateTime GetDateTime (string name)
+		{
+			return (DateTime) GetValue (name, Type.GetType ("System.DateTime"));
+		}
+		
+		public Decimal GetDecimal (string name)
+		{
+			return (Decimal) GetValue (name, Type.GetType ("System.Decimal"));
+		}
+		
+		public double GetDouble (string name)
+		{
+			return (double) GetValue (name, Type.GetType ("System.Double"));
+		}
+						
+		public short GetInt16 (string name)
+		{
+			return (short) GetValue (name, Type.GetType ("System.Int16"));
+		}
+		
+		public int GetInt32 (string name)
+		{
+			return (int) GetValue (name, Type.GetType ("System.Int32"));
+		}
+	       
+		public long GetInt64 (string name)
+		{
+			return (long) GetValue (name, Type.GetType ("System.Int64"));
+		}
+
+		[CLSCompliant(false)]
+		public SByte GetSByte (string name)
+		{
+			return (sbyte) GetValue (name, Type.GetType ("System.SByte"));
+		}
+		
+		public float GetSingle (string name)
+		{
+			return (float) GetValue (name, Type.GetType ("System.SIngle"));
+		}
+		
+		public string GetString (string name)
+		{
+			return (string) GetValue (name, Type.GetType ("System.String"));
+		}
+
+		[CLSCompliant(false)]
+		public UInt16 GetUInt16 (string name)
+		{
+			return (UInt16) GetValue (name, Type.GetType ("System.UInt16"));
+		}
+		
+		[CLSCompliant(false)]
+		public UInt32 GetUInt32 (string name)
+		{
+			return (UInt32) GetValue (name, Type.GetType ("System.UInt32"));
+		}
+		[CLSCompliant(false)]
+		public UInt64 GetUInt64 (string name)
+		{
+			return (UInt64) GetValue (name, Type.GetType ("System.UInt64"));
+		}
 	}
 }

+ 71 - 57
mcs/class/corlib/System.Runtime.Serialization/SerializationInfoEnumerator.cs

@@ -1,57 +1,71 @@
-//
-// System.Runtime.Serialization.SerializationEnumerator.cs
-//
-// Author:
-//   Dan Lewis ([email protected])
-//
-// (C) 2002
-//
-// Stub file. Fix when SerializationInfo is implemented.
-//
-
-using System.Collections;
-
-namespace System.Runtime.Serialization {
-
-	[MonoTODO]
-	public sealed class SerializationInfoEnumerator : IEnumerator {
-		public SerializationEntry Current {
-			get { return new SerializationEntry (); }
-		}
-
-		public string Name {
-			get { return null; }
-		}
-
-		public Type ObjectType {
-			get { return null; }
-		}
-
-		public object Value {
-			get { return null; }
-		}
-
-		// IEnumerator
-
-		object IEnumerator.Current {
-			get { return null; }
-		}
-
-		public bool MoveNext () {
-			return false;
-		}
-
-		public void Reset () {
-		}
-
-		// internal
-
-		internal SerializationInfoEnumerator (SerializationInfo info) {
-			this.info = info;
-		}
-
-		// private
-	
-		private SerializationInfo info;
-	}
-}
+//
+// System.Runtime.Serialization.SerializationInfoEnumerator.cs
+//
+// Author: Duncan Mak ([email protected])
+//
+// (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 {				
+				SerializationEntry entry = (SerializationEntry) ide.Value;
+								
+				return new SerializationEntry (entry.Name, entry.ObjectType, entry.Value);
+			}
+		}
+
+		object IEnumerator.Current
+		{			
+			get { 				
+				SerializationEntry entry = (SerializationEntry) ide.Value;
+				
+				return new SerializationEntry (entry.Name, entry.ObjectType, entry.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 ();
+		}
+	}
+	
+}