i'm trying to make an automapper helper function that checks conversion of enums (where i've custom business logic in mappers)
mapper is an instance, non static.
var cfgExp = new MapperConfigurationExpression();
cfgExp.AddProfile<ProfileXXX>()
MapperConfiguration mapConfig = new MapperConfiguration(cfgExp);
IMapper mapper = new Mapper(mapConfig);
mapper.ConfigurationProvider.AssertConfigurationIsValid();
mapper.AssertEnumConversion(cfgExp);
public static void AssertEnumConversion(this IMapper @thisMapper, MapperConfigurationExpression cfgExp) { try { if (@thisMapper == null) throw new ArgumentNullException(nameof(@thisMapper));
List<TypePair> enumMapping = cfgExp
.Profiles
.SelectMany(x => x.TypeMapConfigs)
.Where(x => x.Types.SourceType.IsEnum)
.Select(x => x.Types)
.ToList();
MethodInfo methodMap = @thisMapper
.GetType()
.GetMethods()
.Where(x => x.Name == "Map" && x.IsGenericMethod)
.ToList()[0];//here i've seen 6 mappers take first //TDestination Map<TDestination>(object source)
foreach (var item in enumMapping)
{
Type tSource = item.SourceType;
Type tDest = item.DestinationType;
//here i've an helper to take a ienumerable<Enum>
MethodInfo method = typeof(EnumConverters).GetMethod("GetEnumValues");
MethodInfo methodGenericSource = method.MakeGenericMethod(tSource);
object enumsSource = methodGenericSource.Invoke(null, null);
IEnumerable<int> enumIenumInt = enumsSource as IEnumerable<int>;
if (enumIenumInt == null)
throw new ApplicationException($"enumIenumInt==null ({tSource.FullName} to {tDest.FullName})");
Array enumArray = Array.CreateInstance(tDest, enumIenumInt.Count());
foreach (var e in enumArray)
{
MethodInfo methodMapGeneric = methodMap.MakeGenericMethod(tDest);
methodMapGeneric.Invoke(@thisMapper, new object[1] { e });//here i've exeption
}
}
}
catch (Exception e)
{
throw;
}
but i receive exception like mapper is not initialized... where i'm wrong!?!?
Aucun commentaire:
Enregistrer un commentaire