2002-08-16 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / SurrogateSelector.cs
1 //
2 // System.Runtime.Serialization.SurrogateSelector.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //
6 // (C) Ximian, Inc.
7 //
8
9 using System;
10 using System.Collections;
11
12 namespace System.Runtime.Serialization
13 {
14         public class SurrogateSelector : ISurrogateSelector
15         {
16                 // Fields
17                 Hashtable Surrogates = new Hashtable ();
18                 string currentKey = null; // current key of Surrogates
19
20                 internal struct Bundle
21                 {
22                         public ISerializationSurrogate surrogate;
23                         public ArrayList selectors;
24
25                         public Bundle (ISerializationSurrogate surrogate)
26                         {
27                                 this.surrogate = surrogate;
28                                 selectors = new ArrayList ();
29                         }
30                 }
31                 
32                 // Constructor
33                 public SurrogateSelector()
34                         : base ()
35                 {
36                 }
37
38                 // Methods
39                 public virtual void AddSurrogate (Type type,
40                           StreamingContext context, ISerializationSurrogate surrogate)
41                 {
42                         if (type == null || surrogate == null)
43                                 throw new ArgumentNullException ("Null reference.");
44
45                         currentKey = type.FullName + "#" + context.ToString ();
46
47                         if (Surrogates.ContainsKey (currentKey))
48                                 throw new ArgumentException ("A surrogate for " + type.FullName + " already exists.");
49
50                         Bundle values = new Bundle (surrogate);
51                         
52                         Surrogates.Add (currentKey, values);
53                 }
54
55                 public virtual void ChainSelector (ISurrogateSelector selector)
56                 {
57                         if (selector == null)
58                                 throw new ArgumentNullException ("Selector is null.");
59                         
60                         Bundle current = (Bundle) Surrogates [currentKey];
61                         current.selectors.Add (selector);
62                 }
63
64                 public virtual ISurrogateSelector GetNextSelector ()
65                 {
66                         Bundle current = (Bundle) Surrogates [currentKey];
67                         return (ISurrogateSelector) current.selectors [current.selectors.Count];
68                 }
69
70                 public virtual ISerializationSurrogate GetSurrogate (Type type,
71                              StreamingContext context, out ISurrogateSelector selector)
72                 {
73                         if (type == null)
74                                 throw new ArgumentNullException ("type is null.");
75                         
76                         string key = type.FullName + "#" + context.ToString ();                 
77                         Bundle current = (Bundle) Surrogates [key];
78                         selector = (ISurrogateSelector) current.selectors [current.selectors.Count - 1];
79                         
80                         return (ISerializationSurrogate) current.surrogate;
81                 }
82
83                 public virtual void RemoveSurrogate (Type type, StreamingContext context)
84                 {
85                         if (type == null)
86                                 throw new ArgumentNullException ("type is null.");
87
88                         string key = type.FullName + "#" + context.ToString ();
89                         Surrogates.Remove (key);
90                 }
91         }
92 }