From 276baea96a414b4e0135bdc0b91de793b3be458e Mon Sep 17 00:00:00 2001
From: mjalil
Date: Mon, 14 Aug 2017 16:35:04 +0430
Subject: [PATCH] add unit test for issue #2266 . remove extra methods. expose
PrimitiveHelper.GetInheritedMember method
---
.../Configuration/Internal/PrimitiveHelper.cs | 5 +----
.../Configuration/MappingExpression.cs | 5 +++--
.../Configuration/PrimitiveExtensions.cs | 3 ---
src/UnitTests/IgnoreAllTests.cs | 17 +++++++++++++++++
4 files changed, 21 insertions(+), 9 deletions(-)
diff --git a/src/AutoMapper/Configuration/Internal/PrimitiveHelper.cs b/src/AutoMapper/Configuration/Internal/PrimitiveHelper.cs
index 555a52e60c..1756554764 100644
--- a/src/AutoMapper/Configuration/Internal/PrimitiveHelper.cs
+++ b/src/AutoMapper/Configuration/Internal/PrimitiveHelper.cs
@@ -17,7 +17,7 @@ public static TValue GetOrDefault(IDictionary dictio
private static IEnumerable GetAllMembers(this Type type) =>
type.GetTypeInheritance().Concat(type.GetTypeInfo().ImplementedInterfaces).SelectMany(i => i.GetDeclaredMembers());
- private static MemberInfo GetInheritedMember(this Type type, string name) => type.GetAllMembers().FirstOrDefault(mi => mi.Name == name);
+ public static MemberInfo GetInheritedMember(this Type type, string name) => type.GetAllMembers().FirstOrDefault(mi => mi.Name == name);
public static MethodInfo GetInheritedMethod(Type type, string name)
=> type.GetInheritedMember(name) as MethodInfo ?? throw new ArgumentOutOfRangeException(nameof(name), $"Cannot find method {name} of type {type}.");
@@ -25,9 +25,6 @@ public static MethodInfo GetInheritedMethod(Type type, string name)
public static MemberInfo GetFieldOrProperty(Type type, string name)
=> type.GetInheritedMember(name) ?? throw new ArgumentOutOfRangeException(nameof(name), $"Cannot find member {name} of type {type}.");
- public static bool HasFieldOrProperty(Type type, string name)
- => type.GetInheritedMember(name) != null;
-
public static bool IsNullableType(Type type)
=> type.IsGenericType(typeof(Nullable<>));
diff --git a/src/AutoMapper/Configuration/MappingExpression.cs b/src/AutoMapper/Configuration/MappingExpression.cs
index 8f4b9d3177..4899abcdb0 100644
--- a/src/AutoMapper/Configuration/MappingExpression.cs
+++ b/src/AutoMapper/Configuration/MappingExpression.cs
@@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;
using AutoMapper.Internal;
+using AutoMapper.Configuration.Internal;
namespace AutoMapper.Configuration
{
@@ -540,8 +541,8 @@ public void Configure(TypeMap typeMap)
var attrs = destProperty.GetCustomAttributes(true);
if (attrs.Any(x => x is IgnoreMapAttribute))
{
- IgnoreDestinationMember(destProperty);
- if (typeMap.SourceType.HasFieldOrProperty(destProperty.Name))
+ IgnoreDestinationMember(destProperty);
+ if (typeMap.SourceType.GetInheritedMember(destProperty.Name) != null)
{
_reverseMap?.ForMember(destProperty.Name, opt => opt.Ignore());
}
diff --git a/src/AutoMapper/Configuration/PrimitiveExtensions.cs b/src/AutoMapper/Configuration/PrimitiveExtensions.cs
index 0816a93a34..e3cfa0310d 100644
--- a/src/AutoMapper/Configuration/PrimitiveExtensions.cs
+++ b/src/AutoMapper/Configuration/PrimitiveExtensions.cs
@@ -19,9 +19,6 @@ public static MethodInfo GetInheritedMethod(this Type type, string name)
public static MemberInfo GetFieldOrProperty(this Type type, string name)
=> PrimitiveHelper.GetFieldOrProperty(type, name);
- public static bool HasFieldOrProperty(this Type type, string name)
- => PrimitiveHelper.HasFieldOrProperty(type, name);
-
public static bool IsNullableType(this Type type)
=> PrimitiveHelper.IsNullableType(type);
diff --git a/src/UnitTests/IgnoreAllTests.cs b/src/UnitTests/IgnoreAllTests.cs
index 9c3abca3c4..43c0d4ef72 100644
--- a/src/UnitTests/IgnoreAllTests.cs
+++ b/src/UnitTests/IgnoreAllTests.cs
@@ -200,5 +200,22 @@ public void Ignore_On_Source_Field()
destination.ShouldNotBeMapped.ShouldBe(null);
}
+
+ public class Source2
+ {
+ }
+
+ public class Destination2
+ {
+ [IgnoreMap]
+ public string ShouldNotThrowExceptionOnReverseMapping { get; set; }
+ }
+
+ [Fact]
+ public void Sould_not_throw_exception_when_reverse_property_does_not_exist()
+ {
+ typeof(ArgumentOutOfRangeException).ShouldNotBeThrownBy(() => new MapperConfiguration(cfg => cfg.CreateMap()
+ .ReverseMap()));
+ }
}
}
\ No newline at end of file