could you please help me in a little bit. Can't find a way of how to update automapper conventions or any other configurations in order to achieve following.
Let's say I have a such classes:
public class A
{
public bool PropertyUpdated {get;set;}
public int PropertyValue {get;set;}
}
and:
public class B
{
public int PropertyValue {get;set;}
}
can I somehow have following map generated automatially (by naming convensions or so)
CreateMap<A,B>()
.ForMember(b => b.PropertyValue, mm =>
{
mm.Condition(a => a.PropertyUpdated);
mm.MapFrom(a => a.PropertyValue);
});
I have a tons of such properties and do not want to write such rules by myself. Just wondering if there's a way to tell automapper that I would like to use some convensional conditions.
Thanks in advance!
thanks @Lucian, it does the trick.
ForAllPropertyMaps(pm => !pm.SourceMember.Name.EndsWith("Updated"), (pm, ce) =>
{
var sourceType = pm.TypeMap.SourceType;
var conditionPropertyName = $"{pm.SourceMember.Name}Specified";
var property = sourceType.GetProperty(conditionPropertyName, BindingFlags.Instance | BindingFlags.Public);
if (property == null) return;
var conditionParameter = Expression.Parameter(typeof(object));
var parameterConvertion = Expression.Convert(conditionParameter, sourceType);
var getPropertyValue = Expression.Property(parameterConvertion, conditionPropertyName);
var lambda = Expression.Lambda<Func<object, bool>>(getPropertyValue, conditionParameter);
ce.Condition(lambda.Compile());
});
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.