浏览代码

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

	* SurrogateSelector.cs: Implemented.

svn path=/trunk/mcs/; revision=2520
Duncan Mak 24 年之前
父节点
当前提交
166b65fc32

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

@@ -1,5 +1,7 @@
 2002-02-19  Duncan Mak  <[email protected]>
 
+	* SurrogateSelector.cs: Implemented.	
+
 	* SerializationInfoEnumerator.cs: oh, and simplified the Current
 	property too.
 	

+ 49 - 10
mcs/class/corlib/System.Runtime.Serialization/SurrogateSelector.cs

@@ -6,10 +6,29 @@
 // (C) Ximian, Inc.
 //
 
+using System;
+using System.Collections;
+
 namespace System.Runtime.Serialization
 {
 	public class SurrogateSelector : ISurrogateSelector
 	{
+		// Fields
+		Hashtable Surrogates = new Hashtable ();
+		string currentKey = null; // current key of Surrogates
+
+		internal struct Bundle
+		{
+			public ISerializationSurrogate surrogate;
+			public ArrayList selectors;
+
+			public Bundle (ISerializationSurrogate surrogate)
+			{
+				this.surrogate = surrogate;
+				selectors = new ArrayList ();
+			}
+		}
+		
 		// Constructor
 		public SurrogateSelector()
 			: base ()
@@ -17,37 +36,57 @@ namespace System.Runtime.Serialization
 		}
 
 		// Methods
-		[MonoTODO]
 		public virtual void AddSurrogate (Type type,
 			  StreamingContext context, ISerializationSurrogate surrogate)
 		{
 			if (type == null || surrogate == null)
-				throw new ArgumentNullException ("Null reference");
+				throw new ArgumentNullException ("Null reference.");
+
+			currentKey = type.FullName + "#" + context.ToString ();
+
+			if (Surrogates.ContainsKey (key))
+				throw new ArgumentException ("A surrogate for " + type.FullName + " already exists.");
+
+			Bundle values = new Bundle (surrogate);
+			
+			Surrogates.Add (currentKey, values);
 		}
 
-		[MonoTODO]
 		public virtual void ChainSelector (ISurrogateSelector selector)
 		{
+			if (selector == null)
+				throw new ArgumentNullException ("Selector is null.");
+			
+			Bundle current = (Bundle) Surrogates [currentKey];
+			current.selectors.Add (selector);
 		}
 
-		[MonoTODO]
 		public virtual ISurrogateSelector GetNextSelector ()
 		{
-			return null;
+			Bundle current = (Bundle) Surrogates [currentKey];
+			return (ISurrogateSelector) current.selectors [current.selectors.Count];
 		}
 
-		[MonoTODO]
 		public virtual ISerializationSurrogate GetSurrogate (Type type,
 			     StreamingContext context, out ISurrogateSelector selector)
 		{
-			selector = null;
-			return null;
+			if (type == null)
+				throw new ArgumentNullException ("type is null.");
+			
+			string key = type.FullName + "#" + context.ToString ();			
+			Bundle current = (Bundle) Surrogates [key];
+			selector = (ISurrogateSelector) current.selectors [current.selectors.Count - 1];
+			
+			return (ISerializationSurrogate) current.surrogate;
 		}
 
-		[MonoTODO]
 		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);
 		}
 	}
-	
 }