英文:
Mapster not mapping derived class in a List c#
问题
I have multiple classes implementing a base class, e.g.:
public abstract class Step
{
public StepId Id {get; set;}
public string Text {get; set;
public StepType Type {get;set;}
}
public class SelectionStep : Step
{
public List<string> Selections {get; set;}
}
public class NumericStep : Step
{
}
With the following Mapster configs:
config.NewConfig<SelectionStep, StepResponse>()
.Map(dest => dest.Selections, src => src.Selections.Select(x => x.Value));
config.NewConfig<Step, StepResponse>()
.Map(dest => dest.Id, src => src.Id.Value.ToString())
.Map(dest => dest.Type, src => src.Type.ToString());
When I map from one SelectionStep
to StepResponse
it's working
_mapper.Map<StepResponse>(SelectionStep)
When I map from List<Step>
containing both SelectionStep
and NumericStep
models, it does not use the SelectionStepConfig
.
_mapper.Map<List<StepResponse>>(List<Step>)
英文:
I have multiple classes implementing a base class, e.g.:
public abstract class Step
{
public StepId Id {get; set;}
public string Text {get; set;
public StepType Type {get;set;}
}
public class SelectionStep : Step
{
public List<string> Selections {get; set;}
}
public class NumericStep : Step
{
}
With the following Mapster configs:
config.NewConfig<SelectionStep, StepResponse>()
.Map(dest => dest.Selections, src => src.Selections.Select(x => x.Value));
config.NewConfig<Step, StepResponse>()
.Map(dest => dest.Id, src => src.Id.Value.ToString())
.Map(dest => dest.Type, src => src.Type.ToString());
When I map from one SelectionStep
to StepResponse
it's working
_mapper.Map<StepResponse>(SelectionStep)
When I map from List<Step>
containing both SelectionStep
and NumericStep
models, it does not use the SelectionStepConfig
.
_mapper.Map<List<StepResponse>>(List<Step>)
答案1
得分: 0
我找到了解决方案。
我将我的 Step
配置更改为包括 SelectionStep
,请参见下面的新配置。
config.NewConfig<SelectionStep, StepResponse>()
.Map(dest => dest.Selections, src => src.Selections.Select(x => x.Value));
config.NewConfig<Step, StepResponse>()
.Include<SelectionStep, StepResponse>()
.Map(dest => dest.Id, src => src.Id.Value.ToString())
.Map(dest => dest.Type, src => src.Type.ToString());
英文:
I found the solution.
I changed my Step
configuration to include the SelectionStep
see below the new configs.
config.NewConfig<SelectionStep, StepResponse>()
.Map(dest => dest.Selections, src => src.Selections.Select(x => x.Value));
config.NewConfig<Step, StepResponse>()
.Include<SelectionStep, StepResponse>()
.Map(dest => dest.Id, src => src.Id.Value.ToString())
.Map(dest => dest.Type, src => src.Type.ToString());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论