5

I Have two functions:

  1. public void DeleteRecord(int id);
  2. public T DeleteRecord<T>(int id);

Here's how i try to dynamically invoke the generic method:

MethodInfo method = typeof(DAL).GetMethod("DeleteRecord", new[] { typeof(int) });
MethodInfo generic = method.MakeGenericMethod(returnType);
object o = generic.Invoke(null, new object[] { dbname, spname, expandoAsDictionary });

The first line throws an exception because it finds an ambiguous definition. Is there a way i can get the MethodInfo of the generic method without using GetMethods and looping through the results asking for IsGenericMethod?

Edit: Please remove the 'Duplicate' because both of the suggested answers either solve this with an inner loop (GetMethods().Select...) or don't even address overloads.

Uri Abramson
  • 6,005
  • 6
  • 40
  • 62
  • This is not a duplicate. i already know how to call a generic method with reflection but i have an ambiguous definition exception. Please read my question through. – Uri Abramson May 28 '13 at 13:52
  • it's a duplicate, but I linked the wrong question, sorry... here's the correct one: http://stackoverflow.com/q/5218395/201088. – Eren Ersönmez May 28 '13 at 14:16
  • 1
    still no dup! i explicitly asked how to do it without using the GetMethods() function. the answer marked in your link is using it... – Uri Abramson May 28 '13 at 14:27

1 Answers1

0

Maybe not the exact solution for your Problem, but maybe you could be happy with this solution:

var generic = typeof(BadFoo).GetMethods().FirstOrDefault(p => p.IsStatic == true && p.Name == "DeleteRecord" && p.ReturnType == typeof(Int32));

I thought there would be like a "FullName" for the Method, so GetMethod() could be calles with a FullName to get the unique Method. But there is no other Property in Method than Name.

Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47
  • Please read my question through. I asked how to do this without GetMethods() :) – Uri Abramson May 28 '13 at 13:53
  • Like I said, this is not the exact solution. But at least you won't be "looping through the results" – Johannes Wanzek May 28 '13 at 13:57
  • Thanks for your answer, i'll considering using it... I'm looking for a way to distinguish between the methods on a higher level as your solution still has a loop behind the scenes (FirstOrDefault) – Uri Abramson May 28 '13 at 14:07