I have several extension methods with same name and different receiving argument:
public static List<T>? MapTo<T>(this List<ClassA_DataModel> data) where T : ClassA_BusinessModel, new() { ... }
public static List<T>? MapTo<T>(this List<ClassB_DataModel> data) where T : ClassB_BusinessModel, new() { ... }
... (+50)
and need to use reflection to invoke the right method according to the List<MyClassX_DataModel> passed as parameter:
var businessObjects = (typeof(MapperModel)?.GetMethod(nameof(MapperModel.MapTo))?.MakeGenericMethod(businessModelType).Invoke(null, new[] { dataObjects }) as IList)?.Cast<object>().ToList();
The problem is that I get an Exception because there are more than one method with the same name:
System.Reflection.AmbiguousMatchException: 'Ambiguous match found'
My guess would be to do something like this:
var myMethod = typeof(MapperModel)?.GetMethods().FirstOrDefault(m => m.Name == nameof(MapperModel.MapTo) && m.XXXX == businessModelType);
var businessObjects = (myMethod.MakeGenericMethod(businessModelType).Invoke(null, new[] { dataObjects }) as IList)?.Cast<object>().ToList();
but I don't know how to get the comparision part to work, provided that I have to match a List<BussinessModel>
. Any suggestion? Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire