Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException when try to map same reference to different type if PreserveReference is true #252

Closed
OsamaAbuSitta opened this issue Jul 20, 2020 · 1 comment

Comments

@OsamaAbuSitta
Copy link

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; }
    }
}
@yyounes
Copy link

yyounes commented Jul 22, 2020

I am facing the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants