Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SurrogateSelector.cs « System.Runtime.Serialization « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec01dfe4d20202da1cec45ee7d1b8d7c43015399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// System.Runtime.Serialization.SurrogateSelector.cs
//
// Author: Duncan Mak (duncan@ximian.com)
//
// (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 ()
		{
		}

		// Methods
		public virtual void AddSurrogate (Type type,
			  StreamingContext context, ISerializationSurrogate surrogate)
		{
			if (type == null || surrogate == null)
				throw new ArgumentNullException ("Null reference.");

			currentKey = type.FullName + "#" + context.ToString ();

			if (Surrogates.ContainsKey (currentKey))
				throw new ArgumentException ("A surrogate for " + type.FullName + " already exists.");

			Bundle values = new Bundle (surrogate);
			
			Surrogates.Add (currentKey, values);
		}

		public virtual void ChainSelector (ISurrogateSelector selector)
		{
			if (selector == null)
				throw new ArgumentNullException ("Selector is null.");
			
			Bundle current = (Bundle) Surrogates [currentKey];
			current.selectors.Add (selector);
		}

		public virtual ISurrogateSelector GetNextSelector ()
		{
			Bundle current = (Bundle) Surrogates [currentKey];
			return (ISurrogateSelector) current.selectors [current.selectors.Count];
		}

		public virtual ISerializationSurrogate GetSurrogate (Type type,
			     StreamingContext context, out ISurrogateSelector selector)
		{
			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;
		}

		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);
		}
	}
}