[英]automapper convert typeof object to enum
I would like to use Automapper to specify a column using ForMember(?) to be an enum value of the type of the object that is being mapped. 我想使用Automapper使用ForMember(?)指定一列作为要映射的对象类型的枚举值。
For example I have 例如我有
class base
class A : base
class B : base
enum objTypeEnum
{
istypebase,
istypea,
istypeb
}
base constains a property 基地包含财产
objTypeEnum TypeEnum;
During the mapping I want to resolve the type of the object being mapped to the TypeEnum property: 在映射期间,我想解析要映射到TypeEnum属性的对象的类型:
.ForMember(dest => dest.TypeEnum, opt => opt.MapFrom()) .ForMember(目的地=> dest.TypeEnum,opt => opt.MapFrom())
Can't figure out the resolver, and how to use it. 无法确定解析器以及如何使用它。 Using the resolver I created, the MapFrom complains that it needs a source definition.
使用我创建的解析器,MapFrom抱怨它需要源定义。
Yes, you can ForMember
method and you define an expression where the argument is the source
. 是的,您可以使用
ForMember
方法,并定义一个以参数为source
的表达式。 So, use the source to provide what you want to set on the destination
property. 因此,使用源提供要在
destination
属性上设置的内容。
.ForMember(dest => dest.TypeEnum,
option => option.MapFrom(source => {
// some logic here based on source object to convert it to enum
// for sample:
if (source.Prop == null)
return TypeEnum.A;
else
return TypeEnum.B;
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.