You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
InvalidCastException when try to map same reference to different type if PreserveReference is true
User the below sample to reproduce it
using Mapster;
using System;
namespace Test
{
internal class Program
{
private static void Main(string[] args)
{
Mapster.TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true);
var employee = new Employee { Id = 1, Name = "Name" };
var department = new Department
{
Manager = employee,
Supervisor = employee
};
// The below line throw : System.InvalidCastException: 'Unable to cast object of type 'Test.EmployeeDto' to type 'Test.PersonDto'.'
var result = department.Adapt<DepartmentDto>();
}
}
public class DepartmentDto
{
public EmployeeDto Manager { get; set; }
public PersonDto Supervisor { get; set; }
}
public class Department
{
public Employee Manager { get; set; }
public Employee Supervisor { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
public class EmployeeDto
{
public int Id { get; set; }
public string Name { get; set; }
}
public class PersonDto
{
public int Id { get; set; }
public string Name { get; set; }
}
}
The text was updated successfully, but these errors were encountered:
InvalidCastException when try to map same reference to different type if PreserveReference is true
User the below sample to reproduce it
The text was updated successfully, but these errors were encountered: