// Copyright 2009-2020 Josh Close and Contributors // 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; using System.Linq; using System.Linq.Expressions; using CsvHelper.Configuration; using CsvHelper.Tests.Mocks; using Xunit; namespace CsvHelper.Tests { public class CsvReaderDynamicMappingsTests { [Fact] public void CanMapDynamics() { var parserMock = new ParserMock { { "IntCol", "StringCol" }, { "1", "one" }, { "2", "two" }, }; var csvReader = new CsvReader(parserMock); csvReader.Context.RegisterClassMap(); var records = csvReader.GetRecords().ToList(); Assert.NotNull(records); Assert.Equal(2, records.Count); Assert.Equal(1, records[0].IntCol); Assert.Equal("one", records[0].StringCol); Assert.Equal(2, records[1].IntCol); Assert.Equal("two", records[1].StringCol); } private class DynamicMappingsType { public int IntCol { get; set; } public string StringCol { get; set; } public static IEnumerable>> Mappings => new List>> { i => i.IntCol, i => i.StringCol }; } private class DynamicMappingsTypeClassMap : ClassMap { public DynamicMappingsTypeClassMap() { foreach (var mapping in DynamicMappingsType.Mappings) { Map(mapping); } } } } }