Skip to content

Commit d296186

Browse files
authored
Merge pull request #3723 from blemasle/inherited-operators-conversion
Inherited source operators support for ConversionOperatorMapper
2 parents 88a25d3 + b131a35 commit d296186

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/AutoMapper/Mappers/ConversionOperatorMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ConversionOperatorMapper : IObjectMapper
1111
public bool IsMatch(TypePair context) => GetConversionOperator(context.SourceType, context.DestinationType) != null;
1212
private MethodInfo GetConversionOperator(Type sourceType, Type destinationType)
1313
{
14-
foreach (MethodInfo sourceMethod in sourceType.GetMember(_operatorName, MemberTypes.Method, TypeExtensions.StaticFlags))
14+
foreach (MethodInfo sourceMethod in sourceType.GetMember(_operatorName, MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy))
1515
{
1616
if (destinationType.IsAssignableFrom(sourceMethod.ReturnType))
1717
{
@@ -26,4 +26,4 @@ public Expression MapExpression(IGlobalConfiguration configurationProvider, Prof
2626
return Expression.Call(conversionOperator, ToType(sourceExpression, conversionOperator.GetParameters()[0].ParameterType));
2727
}
2828
}
29-
}
29+
}

src/UnitTests/Mappers/ConversionOperators.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public static implicit operator string(Foo other)
8282

8383
}
8484

85+
public class InheritedFoo : Foo
86+
{ }
87+
8588
public class Bar
8689
{
8790
public string OtherValue { get; set; }
@@ -97,6 +100,17 @@ public void Should_use_the_implicit_conversion_operator()
97100

98101
_bar.OtherValue.ShouldBe("Hello");
99102
}
103+
104+
[Fact]
105+
public void Should_use_the_inherited_implicit_conversion_operator()
106+
{
107+
var source = new InheritedFoo { Value = "Hello" };
108+
109+
var config = new MapperConfiguration(cfg => { });
110+
_bar = config.CreateMapper().Map<InheritedFoo, Bar>(source);
111+
112+
_bar.OtherValue.ShouldBe("Hello");
113+
}
100114
}
101115

102116
public class When_mapping_to_classes_with_explicit_conversion_operator_on_the_destination
@@ -147,6 +161,9 @@ public static explicit operator Bar(Foo other)
147161
}
148162
}
149163

164+
public class InheritedFoo : Foo
165+
{ }
166+
150167
public class Bar
151168
{
152169
public string OtherValue { get; set; }
@@ -159,5 +176,16 @@ public void Should_use_the_explicit_conversion_operator()
159176
_bar = config.CreateMapper().Map<Foo, Bar>(new Foo { Value = "Hello" });
160177
_bar.OtherValue.ShouldBe("Hello");
161178
}
179+
180+
[Fact]
181+
public void Should_use_the_inherited_explicit_conversion_operator()
182+
{
183+
var source = new InheritedFoo { Value = "Hello" };
184+
185+
var config = new MapperConfiguration(cfg => { });
186+
_bar = config.CreateMapper().Map<InheritedFoo, Bar>(source);
187+
188+
_bar.OtherValue.ShouldBe("Hello");
189+
}
162190
}
163-
}
191+
}

0 commit comments

Comments
 (0)