4

I am attempting to use a Type as a generic. Essentially, I'm able to find a Type based off it's signature using reflection. I now need to do something like this:

Type theType = this.getMyType(mySignature);
string convertedData = mappingObj.mapIt<DataBase, theType>(myData);

However, I cannot use theType as a generic. Is there a way to do this?

EDIT: Following suggestion by Sohaib Jundi, I run into the following (the non simplified code is working with Automapper's Map method):

typeof(IMapper).GetMethod("Map")

That line yields this error:

'typeof(IMapper).GetMethod("Map")' threw an exception of type 'System.Reflection.AmbiguousMatchException'

The method I'm attempting to get with GetMethod is Map<TDestination, TSource>(TSource source), but I cannot determine the method signature to call to get that method. For reference, here is the link to automappers IMapper class, where the Map method lives.

AutoMapper IMapper

dbc
  • 104,963
  • 20
  • 228
  • 340
steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • There is no need for that, just use a non generic overload. – Lucian Bargaoanu Jul 10 '19 at 18:46
  • @LucianBargaoanu Explain please? – steventnorris Jul 10 '19 at 18:54
  • Can you [edit] your question to include a full [mcve] showing how to reproduce your problem? If `mappingObj` has multiple overloaded generic methods named `mapIt` (not shown in your question), see [referencing desired overloaded generic method](https://stackoverflow.com/q/588149) or maybe [How do I use reflection to call a generic method?](https://stackoverflow.com/q/232535/3744182). Or maybe `mappingObj` has a non-generic version that takes types as input arguments? – dbc Jul 10 '19 at 20:11
  • @dbc Added more details on the automapper signature I'm attempting to use. – steventnorris Jul 10 '19 at 20:19
  • Well there's a method `object Map(object source, Type sourceType, Type destinationType);`, why don't you just use that and avoid the generics entirely? There's no `Map(TSource source)` by the way, it's `TDestination Map(TSource source);` (the order of the generics is swapped). – dbc Jul 10 '19 at 20:21
  • 1
    Ah here we go: is your question actually a duplicate of [Using AutoMapper to map unknown types](https://stackoverflow.com/q/14939648)? – dbc Jul 10 '19 at 20:26
  • 1
    @dbc That is actually probably a better way than what I'm doing. Didn't know that map method existed with the types in it. It doesn't exactly solve the original signature question, but it certainly solves my actual problem. – steventnorris Jul 10 '19 at 21:11

1 Answers1

7

You can also use reflection to invoke the generic method. It would be something like this:

string convertedData = (string)mappingObj.GetType().GetMethod("mapIt")
    .MakeGenericMethod(typeof(DataBase), theType).Invoke(mappingObj, new object[] { myData });
Sohaib Jundi
  • 1,576
  • 2
  • 7
  • 15
  • This. According to the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.methodinfo.makegenericmethod?view=netframework-4.8) "Substitutes the elements of an array of types for the type parameters of the current generic method definition, and returns a MethodInfo object representing the resulting constructed method." – Phil Cooper Jul 10 '19 at 15:27
  • I think this is the right way to go, so thanks, but I'm hitting an issue: typeof(IMapper).GetMethod("Map") That line returns 'typeof(IMapper).GetMethod("Map")' threw an exception of type 'System.Reflection.AmbiguousMatchException' before I have a chance to continue down the dot chain. – steventnorris Jul 10 '19 at 18:21
  • use `mappingObj.GetType()` instead of `typeof(IMapper)` as it will get the exact type at runtime – Sohaib Jundi Jul 10 '19 at 18:24
  • So the issue isn't the IMapper, it's the GetMethod signature. I've tracked that much down. Do you know how that GetMethod would look for the method Map(TSource source)? – steventnorris Jul 10 '19 at 19:12
  • the generic agrs are specified with `MakeGenericMethod(typeof(DataBase), theType)` – Sohaib Jundi Jul 10 '19 at 19:25
  • MakeGenericMethod cannot be called because GetMethod is returning null. I've added some details to the OP that should make things clearer. – steventnorris Jul 10 '19 at 20:20