Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/Mapster.Tests/WhenUsingDestinationValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ public void MapUsingDestinationValue()
poco.Strings.ShouldBe(strings);
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/410
/// </summary>
[TestMethod]
public void MappingToReadonlyPropertyWhenPocoDetectRegression()
{
var studentDto = new StudentDtoOrigin { Name = "Marta" };
var student = studentDto.Adapt<StudentOrigin>(); // 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; }
Expand Down
6 changes: 6 additions & 0 deletions src/Mapster/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading