0

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.

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:

.ForMember(dest => 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.

1 Answer 1

0

Yes, you can ForMember method and you define an expression where the argument is the source. So, use the source to provide what you want to set on the destination property.

.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;
           }))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.