0

I would like to call a method based on a string. It works fine using MethodInfo.Invoke().

What I would like to know if there is a way to add parameters to the call, based on the same string, e.g. instead of calling foo() I would like to call foo(1, true, "bar").

I suppose I'd have to extract the parameters, convert them to their correct primitive type and invoke the method using methodInfo.Invoke(classInstance, params).

It feels like a long shot, but is there an "OK" way of solving this?

Johan
  • 35,120
  • 54
  • 178
  • 293
  • 2
    maybe it's will answer you: http://stackoverflow.com/questions/569249/methodinfo-invoke-with-out-parameter and this: http://stackoverflow.com/questions/8779731/how-to-pass-a-parameter-as-a-reference-with-methodinfo-invoke – AsfK Sep 10 '14 at 08:34
  • Can you give more info on the application of this? – Patrick Hofman Sep 10 '14 at 08:34
  • @PatrickHofman It's a webforms app where I would like a user to be able to select a dynamic data source. To be more specific, it's a data source for a crystal report web report parameter. – Johan Sep 10 '14 at 08:38
  • How do the functions differ? – Patrick Hofman Sep 10 '14 at 08:40
  • @PatrickHofman They don't exist yet, but basically I would like the user to have the possibility to override a methods default behavior by passing some optional arguments. – Johan Sep 10 '14 at 08:46
  • Can't you rely on an interface implementation or something like that? – Patrick Hofman Sep 10 '14 at 08:50
  • @PatrickHofman Sure, but regardless of the method signatures, how should the arguments be passed? – Johan Sep 10 '14 at 08:58
  • Maybe as a dictionary? – Patrick Hofman Sep 10 '14 at 08:59

1 Answers1

0

What level of variance are we talking about? Do all parameters need to be completely dynamic, i.e., different types, different number, different order. Or are there a set of optional parameters of known order and type?

If dynamic, then an object dictionary/array is possibly the only solution. Otherwise, known parameters can be declared with default values, making them optional:

public void SomeMethod(int one = 0, string two = "default", bool three = true)
DvS
  • 1,025
  • 6
  • 11
  • The type will be known, though the number may differ, which can be solved with overloads or default values as you're saying. My biggest problem would still be converting values from strings to their correct type. – Johan Sep 10 '14 at 12:20
  • It depends on the usage. Surely parameters passed in will be expected by the code to which they are passed? Each parameter in an object array can be converted to the appropriate type then? How are you using the parameters? – DvS Sep 10 '14 at 12:25
  • To elaborate, the string could look like this: `foo("bar", true)`, which would require me to extract `"bar"` and `true`, add them to an `object[]`, and pass it as the second argument to the `Invoke()` method. I was hoping for an alternative solution, but I can't think of any since I'm stuck with the string – Johan Sep 10 '14 at 12:28
  • I was thinking that it would rather be `foo(object[] values)` and the `foo()` method would pass the array on. So the calling code would simply compose the object array and pass it in. The receiving code (called by foo) could would know how to extract the values it needs. This is a standard mechanism I think. – DvS Sep 10 '14 at 13:27
  • Yeah, that's what I'm thinking as well. But still, the 2 values will have to be extracted and converted to a `new object[] { "bar", true }`. Guess I'll have to split the string or something like that... – Johan Sep 10 '14 at 13:42