using System; using System.Collections.Generic; namespace FullSerializer { /// /// fsContext stores global metadata that can be used to customize how fsConverters operate /// during serialization. /// public sealed class fsContext { /// /// All of the context objects. /// private readonly Dictionary _contextObjects = new Dictionary(); /// /// Removes all context objects from the context. /// public void Reset() { _contextObjects.Clear(); } /// /// Sets the context object for the given type with the given value. /// public void Set(T obj) { _contextObjects[typeof(T)] = obj; } /// /// Returns true if there is a context object for the given type. /// public bool Has() { return _contextObjects.ContainsKey(typeof(T)); } /// /// Fetches the context object for the given type. /// public T Get() { object val; if (_contextObjects.TryGetValue(typeof(T), out val)) { return (T)val; } throw new InvalidOperationException("There is no context object of type " + typeof(T)); } } }