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
{{ message }}
Repository navigation
InvalidCastException when using polymorphic mapping with required properties #906
Mapster.CompileException: Error while compiling
source=AbstractSource
destination=AbstractDestination
type=Map---> System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.NewExpression'.
at Mapster.Adapters.ClassAdapter.CreateInlineExpression(Expressionsource,CompileArgumentarg,BooleanIsRequiredOnly)
at Mapster.Adapters.BaseAdapter.CreateBlockExpressionBody(Expressionsource,Expressiondestination,CompileArgumentarg)
at Mapster.Adapters.BaseAdapter.CreateExpressionBody(Expressionsource,Expressiondestination,CompileArgumentarg)
at Mapster.Adapters.BaseAdapter.CreateAdaptFunc(CompileArgumentarg)
at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgumentarg)---End of inner exception stack trace ---atMapster.TypeAdapterConfig.CreateMapExpression(CompileArgumentarg)
at Mapster.TypeAdapterConfig.CreateMapExpression(TypeTupletuple,MapTypemapType)
at Mapster.TypeAdapterConfig.Compile(BooleanfailFast)
Workaround
Remove the required keyword from destination properties and use default initialisers instead.
Activity
DocSvartz commentedon Mar 31, 2026
Hi @DS-DanielW. Yes, it does have to do with that. Thanks!
fix: MapsterMapper#906 - InvalidCastException when using polymorphic …