AutoMapper/AutoMapper — Issue #144: Use the Mapping Engine to create instances of ctor arguments
Author: pedroreys
Created: 2011-11-07 18:47:46+00:00
State: open

The test below fails with an InvalidCastException: "Unable to cast object of type 'SourceBar' to type 'DestinationBar'"

[TestFixture]
public class UsingMappingEngineToResolveConstructorArguments
{
[Test]
public void Should_resolve_constructor_arguments_using_mapping_engine()
{
Mapper.CreateMap();

Mapper.CreateMap();

var sourceBar = new SourceBar("fooBar");
var sourceFoo = new SourceFoo(sourceBar);

var destinationFoo = Mapper.Map(sourceFoo);

destinationFoo.Bar.FooBar.ShouldEqual(sourceBar.FooBar);
}

public class DestinationFoo
{
public DestinationBar Bar { get; private set; }

public DestinationFoo(DestinationBar bar)
{
Bar = bar;
}
}

public class DestinationBar
{
public string FooBar { get; private set; }

public DestinationBar(string fooBar)
{
FooBar = fooBar;
}
}

public class SourceFoo
{
public SourceBar Bar { get; private set; }

public SourceFoo(SourceBar bar)
{
Bar = bar;
}
}

public class SourceBar
{
public string FooBar { get; private set; }

public SourceBar(string fooBar)
{
FooBar = fooBar;
}
}
}