diff --git a/src/Mapster.Tests/WhenUsingDestinationValue.cs b/src/Mapster.Tests/WhenUsingDestinationValue.cs index 086bf93..f3f7d3a 100644 --- a/src/Mapster.Tests/WhenUsingDestinationValue.cs +++ b/src/Mapster.Tests/WhenUsingDestinationValue.cs @@ -35,6 +35,32 @@ public void MapUsingDestinationValue() poco.Strings.ShouldBe(strings); } + /// + /// https://github.com/MapsterMapper/Mapster/issues/410 + /// + [TestMethod] + public void MappingToReadonlyPropertyWhenPocoDetectRegression() + { + var studentDto = new StudentDtoOrigin { Name = "Marta" }; + var student = studentDto.Adapt(); // No exception. + + student.Name.ShouldBe("John"); + } + + + public class StudentOrigin + { + [UseDestinationValue] + public string Name { get; } = "John"; // only readonly + } + + public class StudentDtoOrigin + { + + public string Name { get; set; } + } + + public class ContractingParty { public string Name { get; set; } diff --git a/src/Mapster/Utils/ReflectionUtils.cs b/src/Mapster/Utils/ReflectionUtils.cs index dae413d..ae12815 100644 --- a/src/Mapster/Utils/ReflectionUtils.cs +++ b/src/Mapster/Utils/ReflectionUtils.cs @@ -61,6 +61,12 @@ public static bool IsPoco(this Type type) if (type.IsConvertible()) return false; + if (type == typeof(Type) || type.BaseType == typeof(MulticastDelegate)) + return false; + + if (type.IsClass && type.GetProperties().Count() != 0) + return true; + return type.GetFieldsAndProperties().Any(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) != 0); }