Skip to content

Instantly share code, notes, and snippets.

@qswinson
Last active July 1, 2016 14:30
Show Gist options
  • Save qswinson/20262e54ff597b757d7500a82301f4a9 to your computer and use it in GitHub Desktop.
Save qswinson/20262e54ff597b757d7500a82301f4a9 to your computer and use it in GitHub Desktop.
AutoMapper 5.0 Bug
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C6FB105A-7FBB-4B7B-81E3-7021F92CE89C}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AutoMapperBug</RootNamespace>
<AssemblyName>AutoMapperBug</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoMapper, Version=5.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>.\packages\AutoMapper.5.0.0\lib\net45\AutoMapper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Should, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>.\packages\Should.1.1.20\lib\Should.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Device" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapperBug", "AutoMapperBug.csproj", "{C6FB105A-7FBB-4B7B-81E3-7021F92CE89C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C6FB105A-7FBB-4B7B-81E3-7021F92CE89C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6FB105A-7FBB-4B7B-81E3-7021F92CE89C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6FB105A-7FBB-4B7B-81E3-7021F92CE89C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6FB105A-7FBB-4B7B-81E3-7021F92CE89C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="5.0.0" targetFramework="net45" />
<package id="Should" version="1.1.20" targetFramework="net45" />
</packages>
using System;
using System.Device.Location;
using AutoMapper;
using Should;
namespace AutoMapperBug
{
class Program
{
static void Main(string[] args)
{
try
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new GeoCoordinateProfile());
});
Mapper.Configuration.AssertConfigurationIsValid();
MapToGeolocationDTO();
Console.WriteLine("GeolocationDTO mapping good");
Console.WriteLine();
MapToGeoCoordinate();
Console.WriteLine("GeoCoordinate mapping good");
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("Done...");
Console.ReadLine();
}
public static void MapToGeolocationDTO()
{
var source = new GeoCoordinate
{
Latitude = 34d,
Longitude = -93d,
HorizontalAccuracy = 100
};
var dest = Mapper.Map<GeolocationDTO>(source);
dest.Latitude.ShouldEqual(source.Latitude);
dest.Longitude.ShouldEqual(source.Longitude);
}
public static void MapToGeoCoordinate()
{
var source = new GeolocationDTO
{
Latitude = 34d,
Longitude = -93d,
HorizontalAccuracy = 100
};
var typeMap = Mapper.Configuration.FindTypeMapFor<GeolocationDTO, GeoCoordinate>();
var dest = Mapper.Map<GeoCoordinate>(source);
dest.Latitude.ShouldEqual(source.Latitude);
dest.Longitude.ShouldEqual(source.Longitude);
}
}
public class GeolocationDTO
{
public double Longitude { get; set; }
public double Latitude { get; set; }
public double? HorizontalAccuracy { get; set; }
}
public class GeoCoordinateProfile : Profile
{
public GeoCoordinateProfile()
{
CreateMap<GeoCoordinate, GeolocationDTO>();
CreateMap<GeolocationDTO, GeoCoordinate>()
.ForMember(dest => dest.Altitude, opt => opt.Ignore())
.ForMember(dest => dest.VerticalAccuracy, opt => opt.Ignore())
.ForMember(dest => dest.Speed, opt => opt.Ignore())
.ForMember(dest => dest.Course, opt => opt.Ignore());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment