using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using AutoMapper.Configuration;
public partial class MyClass
public string A { get; set; }
public string B { get; set; }
public partial class MyClass
public string IgnoreMe { get; set; }
[JsonProperty("LongNumber")]
public partial class MyClassDTO
public string C { get; set; }
public string D { get; set; }
public partial class MyClassDTO
public string IgnoreMe { get; set; }
public string SomethingNotInTheOriginalClass { get; set; }
public long LongNumber { get; set; }
public static class AutomapperJsonExtensions
static readonly IContractResolver defaultResolver = new JsonSerializer().ContractResolver;
public static void CreateJsonObjectMap<TSource, TDestination>(this IMapperConfigurationExpression cfg, IContractResolver resolver = null)
resolver = resolver ?? defaultResolver;
var srcContract = resolver.ResolveContract(typeof(TSource)) as JsonObjectContract ?? throw new ArgumentException("Source was not a JSON object.");
var dstMemberQuery = typeof(TDestination).GetProperties()
.Where(p => p.GetIndexParameters().Length == 0 && p.GetGetMethod(true) != null && p.CanRead && p.GetSetMethod() != null && p.CanWrite)
.Concat(typeof(TDestination).GetFields().Select(f => f.Name));
var map = cfg.CreateMap<TSource, TDestination>();
foreach (var member in dstMemberQuery)
map.ForMember(member, opt =>
var property = srcContract.Properties.GetClosestMatchProperty(member);
if (property == null || property.Ignored)
opt.MapFrom(property.UnderlyingName);
public static void Test()
var config = new MapperConfiguration(cfg =>
cfg.CreateJsonObjectMap<MyClass, MyClassDTO>();
var mapper = config.CreateMapper();
SomeField = "value of SomeField",
LongField = long.MaxValue,
var dto = mapper.Map<MyClassDTO>(source);
Assert.AreEqual(source.A, dto.C);
Assert.AreEqual(source.B, dto.D);
Assert.AreEqual(null, dto.IgnoreMe);
Assert.AreEqual(source.SomeField, dto.SomeField);
Assert.AreEqual(null, dto.SomethingNotInTheOriginalClass);
Assert.AreEqual(source.LongField, dto.LongNumber);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("{0} version: {1}", typeof(MapperConfiguration).Assembly.GetName().Name, typeof(MapperConfiguration).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];