Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AutoMapper/Mappers/ConversionOperatorMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ConversionOperatorMapper : IObjectMapper
public bool IsMatch(TypePair context) => GetConversionOperator(context.SourceType, context.DestinationType) != null;
private MethodInfo GetConversionOperator(Type sourceType, Type destinationType)
{
foreach (MethodInfo sourceMethod in sourceType.GetMember(_operatorName, MemberTypes.Method, TypeExtensions.StaticFlags))
foreach (MethodInfo sourceMethod in sourceType.GetMember(_operatorName, MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy))
{
if (destinationType.IsAssignableFrom(sourceMethod.ReturnType))
{
Expand All @@ -26,4 +26,4 @@ public Expression MapExpression(IGlobalConfiguration configurationProvider, Prof
return Expression.Call(conversionOperator, ToType(sourceExpression, conversionOperator.GetParameters()[0].ParameterType));
}
}
}
}
30 changes: 29 additions & 1 deletion src/UnitTests/Mappers/ConversionOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public static implicit operator string(Foo other)

}

public class InheritedFoo : Foo
{ }

public class Bar
{
public string OtherValue { get; set; }
Expand All @@ -97,6 +100,17 @@ public void Should_use_the_implicit_conversion_operator()

_bar.OtherValue.ShouldBe("Hello");
}

[Fact]
public void Should_use_the_inherited_implicit_conversion_operator()
{
var source = new InheritedFoo { Value = "Hello" };

var config = new MapperConfiguration(cfg => { });
_bar = config.CreateMapper().Map<InheritedFoo, Bar>(source);

_bar.OtherValue.ShouldBe("Hello");
}
}

public class When_mapping_to_classes_with_explicit_conversion_operator_on_the_destination
Expand Down Expand Up @@ -147,6 +161,9 @@ public static explicit operator Bar(Foo other)
}
}

public class InheritedFoo : Foo
{ }

public class Bar
{
public string OtherValue { get; set; }
Expand All @@ -159,5 +176,16 @@ public void Should_use_the_explicit_conversion_operator()
_bar = config.CreateMapper().Map<Foo, Bar>(new Foo { Value = "Hello" });
_bar.OtherValue.ShouldBe("Hello");
}

[Fact]
public void Should_use_the_inherited_explicit_conversion_operator()
{
var source = new InheritedFoo { Value = "Hello" };

var config = new MapperConfiguration(cfg => { });
_bar = config.CreateMapper().Map<InheritedFoo, Bar>(source);

_bar.OtherValue.ShouldBe("Hello");
}
}
}
}