简体   繁体   中英

Automapper ignore property on generics mapping

This generic method works fine :

public static U PropertyAutomapper<T, U>(T source)
    where T : class, new()
    where U : class, new()
{
    Mapper.CreateMap(typeof(T), typeof(U));
    return Mapper.Map<T, U>(source);
}

I have this interface :

public interface IPassword
{
    string Password { get; set; }
}

I'd like ignore this property (' Password ')but I don't have 'ignore' in the intelissense

public static U PropertyAutomapperNoPassword<T, U>(T source)
    where T : IPassword
    where U : IPassword
{
    Mapper.CreateMap(typeof(T), typeof(U))...   
    return Mapper.Map<T, U>(source);
}

Any idea ?

Thanks,

Try this:

Mapper.CreateMap<T, U>()
    .ForMember(dest => dest.Password, opt => opt.Ignore())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM