mscorlib(4.0.0.0) API with additions
SurrogateSelector.cs
2 using System.Security;
3 
5 {
7  [ComVisible(true)]
9  {
10  internal SurrogateHashtable m_surrogates;
11 
12  internal ISurrogateSelector m_nextSelector;
13 
16  {
17  m_surrogates = new SurrogateHashtable(32);
18  }
19 
26  public virtual void AddSurrogate(Type type, StreamingContext context, ISerializationSurrogate surrogate)
27  {
28  if (type == null)
29  {
30  throw new ArgumentNullException("type");
31  }
32  if (surrogate == null)
33  {
34  throw new ArgumentNullException("surrogate");
35  }
36  SurrogateKey key = new SurrogateKey(type, context);
37  m_surrogates.Add(key, surrogate);
38  }
39 
40  [SecurityCritical]
41  private static bool HasCycle(ISurrogateSelector selector)
42  {
43  ISurrogateSelector surrogateSelector = selector;
44  ISurrogateSelector surrogateSelector2 = selector;
45  while (surrogateSelector != null)
46  {
47  surrogateSelector = surrogateSelector.GetNextSelector();
48  if (surrogateSelector == null)
49  {
50  return true;
51  }
52  if (surrogateSelector == surrogateSelector2)
53  {
54  return false;
55  }
56  surrogateSelector = surrogateSelector.GetNextSelector();
57  surrogateSelector2 = surrogateSelector2.GetNextSelector();
58  if (surrogateSelector == surrogateSelector2)
59  {
60  return false;
61  }
62  }
63  return true;
64  }
65 
71  [SecurityCritical]
72  public virtual void ChainSelector(ISurrogateSelector selector)
73  {
74  if (selector == null)
75  {
76  throw new ArgumentNullException("selector");
77  }
78  if (selector == this)
79  {
80  throw new SerializationException(Environment.GetResourceString("Serialization_DuplicateSelector"));
81  }
82  if (!HasCycle(selector))
83  {
84  throw new ArgumentException(Environment.GetResourceString("Serialization_SurrogateCycleInArgument"), "selector");
85  }
86  ISurrogateSelector nextSelector = selector.GetNextSelector();
87  ISurrogateSelector surrogateSelector = selector;
88  while (nextSelector != null && nextSelector != this)
89  {
90  surrogateSelector = nextSelector;
91  nextSelector = nextSelector.GetNextSelector();
92  }
93  if (nextSelector == this)
94  {
95  throw new ArgumentException(Environment.GetResourceString("Serialization_SurrogateCycle"), "selector");
96  }
97  nextSelector = selector;
98  ISurrogateSelector surrogateSelector2 = selector;
99  while (nextSelector != null)
100  {
101  nextSelector = ((nextSelector != surrogateSelector) ? nextSelector.GetNextSelector() : GetNextSelector());
102  if (nextSelector == null)
103  {
104  break;
105  }
106  if (nextSelector == surrogateSelector2)
107  {
108  throw new ArgumentException(Environment.GetResourceString("Serialization_SurrogateCycle"), "selector");
109  }
110  nextSelector = ((nextSelector != surrogateSelector) ? nextSelector.GetNextSelector() : GetNextSelector());
111  surrogateSelector2 = ((surrogateSelector2 != surrogateSelector) ? surrogateSelector2.GetNextSelector() : GetNextSelector());
112  if (nextSelector == surrogateSelector2)
113  {
114  throw new ArgumentException(Environment.GetResourceString("Serialization_SurrogateCycle"), "selector");
115  }
116  }
117  ISurrogateSelector nextSelector2 = m_nextSelector;
118  m_nextSelector = selector;
119  if (nextSelector2 != null)
120  {
121  surrogateSelector.ChainSelector(nextSelector2);
122  }
123  }
124 
128  [SecurityCritical]
130  {
131  return m_nextSelector;
132  }
133 
141  [SecurityCritical]
143  {
144  if (type == null)
145  {
146  throw new ArgumentNullException("type");
147  }
148  selector = this;
149  SurrogateKey key = new SurrogateKey(type, context);
150  ISerializationSurrogate serializationSurrogate = (ISerializationSurrogate)m_surrogates[key];
151  if (serializationSurrogate != null)
152  {
153  return serializationSurrogate;
154  }
155  if (m_nextSelector != null)
156  {
157  return m_nextSelector.GetSurrogate(type, context, out selector);
158  }
159  return null;
160  }
161 
166  public virtual void RemoveSurrogate(Type type, StreamingContext context)
167  {
168  if (type == null)
169  {
170  throw new ArgumentNullException("type");
171  }
172  SurrogateKey key = new SurrogateKey(type, context);
173  m_surrogates.Remove(key);
174  }
175  }
176 }
virtual void ChainSelector(ISurrogateSelector selector)
Adds the specified T:System.Runtime.Serialization.ISurrogateSelector that can handle a particular obj...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector)
Returns the surrogate for a particular type.
ISurrogateSelector GetNextSelector()
Returns the next surrogate selector in the chain.
Definition: __Canon.cs:3
ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector)
Finds the surrogate that represents the specified object's type, starting with the specified surrogat...
virtual void RemoveSurrogate(Type type, StreamingContext context)
Removes the surrogate associated with a given type.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
SurrogateSelector()
Initializes a new instance of the T:System.Runtime.Serialization.SurrogateSelector class.
virtual void AddSurrogate(Type type, StreamingContext context, ISerializationSurrogate surrogate)
Adds a surrogate to the list of checked surrogates.
Implements a serialization surrogate selector that allows one object to perform serialization and des...
void ChainSelector(ISurrogateSelector selector)
Specifies the next T:System.Runtime.Serialization.ISurrogateSelector for surrogates to examine if the...
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
The exception thrown when an error occurs during serialization or deserialization.
Indicates a serialization surrogate selector class.
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual ISurrogateSelector GetNextSelector()
Returns the next selector on the chain of selectors.
Assists formatters in selection of the serialization surrogate to delegate the serialization or deser...