MapsterMapper / Mapster

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

没有包含任何属性的class映射失败 #585

Closed liuqing19851022 closed 1 year ago

liuqing19851022 commented 1 year ago
public class TestEmpty
{
}

public class SubEntity : TestEmpty
{ }

TestEmpty testEmpty = new TestEmpty();
SubEntity subEntity = testEmpty.Adapt<SubEntity>();

System.InvalidCastException: Unable to cast object of type 'Sample.AspNetCore.Models.TestEmpty' to type 'Sample.AspNetCore.Models.SubEntity'. at lambda_method12(Closure , Object )

stagep commented 1 year ago

You can not cast from a base type to a derived type. There is no guarantee that TestEmpty is of type SubEntity. You can however cast to the base class as follows:

SubEntity subEntity = new SubEntity();
TestEmpty testEmpty = subEntity.Adapt<TestEmpty>();