// 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 CsvHelper.Configuration;
using CsvHelper.TypeConversion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsvHelper
{
///
/// Share state for CsvHelper.
///
public class CsvContext
{
///
/// Gets or sets the .
///
public virtual TypeConverterOptionsCache TypeConverterOptionsCache { get; set; } = new TypeConverterOptionsCache();
///
/// Gets or sets the .
///
public virtual TypeConverterCache TypeConverterCache { get; set; } = new TypeConverterCache();
///
/// The configured s.
///
public virtual ClassMapCollection Maps { get; private set; }
///
/// Gets the parser.
///
public IParser Parser { get; private set; }
///
/// Gets the reader.
///
public IReader Reader { get; internal set; }
///
/// Gets the writer.
///
public IWriter Writer { get; internal set; }
///
/// Gets the configuration.
///
public CsvConfiguration Configuration { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The reader.
public CsvContext(IReader reader)
{
Reader = reader;
Parser = reader.Parser;
Configuration = reader.Configuration as CsvConfiguration ?? throw new InvalidOperationException($"{nameof(IReader)}.{nameof(IReader.Configuration)} must be of type {nameof(CsvConfiguration)} to be used in the context.");
Maps = new ClassMapCollection(this);
}
///
/// Initializes a new instance of the class.
///
/// The parser.
public CsvContext(IParser parser)
{
Parser = parser;
Configuration = parser.Configuration as CsvConfiguration ?? throw new InvalidOperationException($"{nameof(IParser)}.{nameof(IParser.Configuration)} must be of type {nameof(CsvConfiguration)} to be used in the context.");
Maps = new ClassMapCollection(this);
}
///
/// Initializes a new instance of the class.
///
/// The writer.
public CsvContext(IWriter writer)
{
Writer = writer;
Configuration = writer.Configuration as CsvConfiguration ?? throw new InvalidOperationException($"{nameof(IWriter)}.{nameof(IWriter.Configuration)} must be of type {nameof(CsvConfiguration)} to be used in the context.");
Maps = new ClassMapCollection(this);
}
///
/// Initializes a new instance of the class.
///
/// The configuration.
public CsvContext(CsvConfiguration configuration)
{
Configuration = configuration;
Maps = new ClassMapCollection(this);
}
///
/// Use a to configure mappings.
/// When using a class map, no members are mapped by default.
/// Only member specified in the mapping are used.
///
/// The type of mapping class to use.
public virtual TMap RegisterClassMap() where TMap : ClassMap
{
var map = ObjectResolver.Current.Resolve();
RegisterClassMap(map);
return map;
}
///
/// Use a to configure mappings.
/// When using a class map, no members are mapped by default.
/// Only members specified in the mapping are used.
///
/// The type of mapping class to use.
public virtual ClassMap RegisterClassMap(Type classMapType)
{
if (!typeof(ClassMap).IsAssignableFrom(classMapType))
{
throw new ArgumentException("The class map type must inherit from CsvClassMap.");
}
var map = (ClassMap)ObjectResolver.Current.Resolve(classMapType);
RegisterClassMap(map);
return map;
}
///
/// Registers the class map.
///
/// The class map to register.
public virtual void RegisterClassMap(ClassMap map)
{
if (map.MemberMaps.Count == 0 && map.ReferenceMaps.Count == 0 && map.ParameterMaps.Count == 0)
{
throw new ConfigurationException("No mappings were specified in the CsvClassMap.");
}
Maps.Add(map);
}
///
/// Unregisters the class map.
///
/// The map type to unregister.
public virtual void UnregisterClassMap()
where TMap : ClassMap
{
UnregisterClassMap(typeof(TMap));
}
///
/// Unregisters the class map.
///
/// The map type to unregister.
public virtual void UnregisterClassMap(Type classMapType)
{
Maps.Remove(classMapType);
}
///
/// Unregisters all class maps.
///
public virtual void UnregisterClassMap()
{
Maps.Clear();
}
///
/// Generates a for the type.
///
/// The type to generate the map for.
/// The generate map.
public virtual ClassMap AutoMap()
{
var map = ObjectResolver.Current.Resolve>();
map.AutoMap(this);
Maps.Add(map);
return map;
}
///
/// Generates a for the type.
///
/// The type to generate for the map.
/// The generate map.
public virtual ClassMap AutoMap(Type type)
{
var mapType = typeof(DefaultClassMap<>).MakeGenericType(type);
var map = (ClassMap)ObjectResolver.Current.Resolve(mapType);
map.AutoMap(this);
Maps.Add(map);
return map;
}
}
}