Yeah, I got it i can use
public class MappingUtility<tfrom,>
{
public static TTo Map(TFrom fromModel)
{
CreateMapRecursive(typeof(TFrom), typeof(TTo));
return Mapper.Map<tfrom,>(fromModel);
}
public static IList<tto> Map(IList<tfrom> fromModels)
{
CreateMapRecursive(typeof(TFrom), typeof(TTo));
return Mapper.Map<ilist><tfrom>, IList<tto>>(fromModels);
}
private static void CreateMapRecursive(Type typeFrom, Type typeTo)
{
var mapToPropertyInfos =
typeTo.GetProperties().Where(
p => p.GetCustomAttributes(typeof(AutoMapperMapToAttribute), false).Any());
Mapper.CreateMap(typeFrom, typeTo);
foreach(var propertyInfo in mapToPropertyInfos)
{
var typeString = ((AutoMapperMapToAttribute)
propertyInfo.GetCustomAttributes(typeof(AutoMapperMapToAttribute), false).Single()).MapToTypeName;
var newFromType = System.Reflection.Assembly.GetAssembly(typeFrom).GetType(typeString);
var newToType = propertyInfo.PropertyType.IsGenericType
? propertyInfo.PropertyType.GetGenericArguments()[0]
: propertyInfo.PropertyType;
if(newFromType == null || newToType == null)
{
throw new InvalidOperationException("Type not recognized;");
}
CreateMapRecursive(newFromType, newToType);
}
}
}
</tto></tfrom></ilist></tfrom></tto>
and Mapping Attribute
public class AutoMapperMapToAttribute : Attribute
{
public AutoMapperMapToAttribute(string mapToTypeName)
{
MapToTypeName = mapToTypeName;
}
public string MapToTypeName { get; set; }
}