// Copyright 2009-2022 Josh Close // This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. // See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. // https://github.com/JoshClose/CsvHelper using System; using System.Collections.Generic; namespace CsvHelper.TypeConversion { /// /// Caches for a given type. /// public class TypeConverterOptionsCache { private Dictionary typeConverterOptions = new Dictionary(); /// /// Adds the for the given . /// /// The type the options are for. /// The options. public void AddOptions(Type type, TypeConverterOptions options) { if (type == null) { throw new ArgumentNullException(nameof(type)); } typeConverterOptions[type] = options ?? throw new ArgumentNullException(nameof(options)); } /// /// Adds the for the given . /// /// The type the options are for. /// The options. public void AddOptions(TypeConverterOptions options) { AddOptions(typeof(T), options); } /// /// Adds the given to all registered types. /// /// public void AddOptions(TypeConverterOptions options) { foreach (var type in typeConverterOptions.Keys) { typeConverterOptions[type] = options; } } /// /// Removes the for the given type. /// /// The type to remove the options for. public void RemoveOptions(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } typeConverterOptions.Remove(type); } /// /// Removes the for the given type. /// /// The type to remove the options for. public void RemoveOptions() { RemoveOptions(typeof(T)); } /// /// Get the for the given . /// /// The type the options are for. /// The options for the given type. public TypeConverterOptions GetOptions(Type type) { if (type == null) { throw new ArgumentNullException(); } if (!typeConverterOptions.TryGetValue(type, out var options)) { options = new TypeConverterOptions(); typeConverterOptions.Add(type, options); } return options; } /// /// Get the for the given . /// /// The type the options are for. /// The options for the given type. public TypeConverterOptions GetOptions() { return GetOptions(typeof(T)); } } }