MapsterMapper / Mapster

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

When there is an error in property mapping, can this property be ignored? #734

Open wangx036 opened 1 week ago

wangx036 commented 1 week ago

For example, in the following code, when objSource.Adapt<DestClass>() is executed, an exception will be thrown. Can we ignore this exception and set objDest.Id = default(int) to obtain objDest as {Id=0,Name="TestName"}?

var objSource = new SourceClass {Id="abc",Name="TestName"};
var objDest = objSource.Adapt<DestClass>();

public class SourceClass
{
    public string Id { get; set; }
    public string Name { get; set; }
}
public class DestClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}
vchc commented 1 week ago

If you want non standard mapping just create a config for your types. Or use [AdaptIgnore] on 'Id' field if you can decorate your class with attributes. It is your custom logic. Why would you want Mapster handle this for you?