[英]AutoMapper Custom Mappings
Lets assume I have the following classes假设我有以下课程
public class foo
{
public string Value;
}
public class bar
{
public string Value1;
public string Value2;
}
Now I want to configure Auto Map, to Map Value1 to Value if Value1 starts with "A", but otherwise I want to map Value2 to Value.现在我想配置自动映射,如果 Value1 以“A”开头,则将 Value1 映射到 Value,否则我想将 Value2 映射到 Value。
This is what I have so far:这是我到目前为止:
Mapper
.CreateMap<foo,bar>()
.ForMember(t => t.Value,
o =>
{
o.Condition(s =>
s.Value1.StartsWith("A"));
o.MapFrom(s => s.Value1);
<<***I want to throw Exception here***>>
})
However I know how can I give value 1 or value 2 on Conditional basis but don't know how to put some custom code , call a function or throw an Exception但是我知道如何在条件基础上给出值 1 或值 2 但不知道如何放置一些自定义代码、调用函数或抛出异常
Please Guide.请指导。
You can pass a lambda to ResolveUsing
:您可以将 lambda 传递给
ResolveUsing
:
.ForMember(f => f.Value, o => o.ResolveUsing(b =>
{
if (b.Value1.StartsWith("A"))
{
return b.Value1;
}
return b.Value2;
}
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.