MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.29k stars 327 forks source link

Cannot map between two types with multiple inheritance #598

Open FaustoNascimento opened 1 year ago

FaustoNascimento commented 1 year ago

I'm trying to use Mapster to map an object from CcmApplication to RpcCcmApplication and getting the following exception:

TypeLoadException: Signature of the body and declaration in a method implementation do not match. Type: 'GeneratedType_1'. Assembly: 'Mapster.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

My code is as follows (sorry for so many classes/interfaces but somehow if I reduce it it actually does work so this is the smallest example I can give):

public class Test
{
    private readonly IMapper _mapper = new Mapper();
    [Fact]
    public void FailedTest()
    {
        var app = new CcmApplication() { DeploymentTypes = new[] { new CcmDeploymentType() { Name = "Test" } } };

        var t = _mapper.Map<RpcCcmApplication>(app);
    }
}

public interface ISystemProperties
{
}

public interface IBaseProperties
{
    public ISystemProperties SystemProperties { get; init; }
}

public interface ICcmSoftwareBase : IBaseProperties
{
    string? Name { get; init; }
}

public interface ICcmApplication : ICcmSoftwareBase
{
    IEnumerable<ICcmDeploymentType>? DeploymentTypes { get; init; }
}

public interface ICcmDeploymentType : ICcmSoftwareBase
{
}

public record SystemProperties : ISystemProperties
{
}

public abstract record BaseProperties : IBaseProperties
{
    public ISystemProperties SystemProperties { get; init; } = null!;
}

public record CcmSoftwareBase : BaseProperties, ICcmSoftwareBase
{
    public string? Name { get; init; }
}

public record CcmApplication : CcmSoftwareBase, ICcmApplication
{
    public IEnumerable<ICcmDeploymentType>? DeploymentTypes { get; init; }
}

public record CcmDeploymentType : CcmSoftwareBase, ICcmDeploymentType
{
}

public record RpcSystemProperties : ISystemProperties
{
}

public abstract record RpcBaseProperties : IBaseProperties
{
    public ISystemProperties SystemProperties { get; init; } = null!;
}

public record RpcCcmSoftwareBase : RpcBaseProperties, ICcmSoftwareBase
{
    public string? Name { get; init; }
}

public record RpcCcmApplication : RpcCcmSoftwareBase, ICcmApplication
{
    public IEnumerable<ICcmDeploymentType>? DeploymentTypes { get; init; }
}

public record RpcCcmDeploymentType : RpcCcmSoftwareBase, ICcmDeploymentType
{
}

What I've noticed is that if ICcmDeploymentType does not implement ICcmSoftwareBase then it works, not sure what I'm missing here.

I've also tried the following configuration, but fails on Compile()

_mapper.Config.ForType<ICcmApplication, RpcCcmApplication>();

_mapper.Config.Compile();

I'm unsure if this is a bug or some problem with my configuration/classes, help would be appreciated.