142

I am invoking a static method Parse on a type via reflection because I do not know the type of the object at compile-time (I do know, however, it has a Parse method, taking a string).

However, I am getting an ambiguous match exception, presumably because there are a lot of overloaded Parse methods each taking a single object (string, int, double etc.).

How can I be more specific in my method invocation to ensure I reach the correct method (Parse(string s)) and the exception is not thrown.

My code looks like this:

Type returnType = p.PropertyType;
object value = returnType.GetMethod("Parse").Invoke(null, new string[] { "1" });
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
Ben Aston
  • 53,718
  • 65
  • 205
  • 331

1 Answers1

234

Use this overload and use

returnType.GetMethod("Parse", new [] {typeof(string)})
Benjamin Podszun
  • 9,679
  • 3
  • 34
  • 45
  • 10
    @Bitterblue I'm confused why you'd write that comment - and why you consider matters of style worth discussing? – Benjamin Podszun Dec 01 '16 at 16:32
  • 10
    The presentation is fine, it just happens to follow a different style than the one you seem to prefer. You commented along the lines of "I'd use this bike shed, if it were a bit more blueish". Which confuses me. Editing the comment: Unless you're unaware that new [] {} actually infers the type of the Array and _IS_ equivalent to new Type[] in this case? In that case I'm sorry - I assumed that you comment on the style (both works) while potentially thinking the snippet is wrong (it isn't). – Benjamin Podszun Dec 02 '16 at 11:49
  • 2
    Thanks for the answer, guys. To save the next guy some trouble, for reference types, use something like this: typeof(string).MakeByRefType(); – BRebey Dec 24 '16 at 00:22
  • @Bitterblue What, you don't like type inference? I actually prefer it the way Benjamin formatted it. I prefer type inference (it's cleaner) than explicit typing. His answer is good, style shouldn't play a factor in the quality of an answer. The result of the answer is the only outcome that matters. – Kris Coleman Mar 24 '17 at 19:51
  • 10
    @Bitterblue I'm not 'young' and that sure sounds condescending. A programmer that cannot read ```new [] { typeof(string) }``` has other issues than maintaining code. The type is literally right there. Plus 'crazy one-liners' aren't relevant, your preferred style just adds redundant letters into this very line. I'd argue that ```new Type[] {...}``` is less readable, because the line's longer and that's irrelevant information/boilerplate/noise. Hence: It's a matter of style and you started the discussion with a passive aggressive 'would have upvoted, if it would cater to my taste'.. – Benjamin Podszun Apr 05 '17 at 15:57
  • @Bitterblue Get some experience? I've been in my career for 9 years, almost a decade. I have plenty already (and plenty more to learn), but I know enough that verbosity is a terrible way to grade an answer. – Kris Coleman Apr 05 '17 at 20:55
  • 3
    Be careful, it will not work if 2 methods have the same names, same number of parameters and same types of parameters. I'm thinking here of explicit cast operators overloads. For example `public static explicit double(MyType obj)` and `public static explicit float(MyType obj)`. You will still have an `AmbiguousMatchException`. In this case, you could use `returnType.GetMethods().SingleOrDefault(m => m.Name == "op_Explicit" && m.ReturnType == typeof(float))` for example. – Guillaume Sep 23 '18 at 23:18
  • 2
    All these comments except BRebey's and Guillame's are completely irrelevant and distracting, please delete them – reggaeguitar Feb 26 '21 at 20:56