1

Difficult to give correct title, sorry for this.

I have a class (I cannot modify) with a function

f(string arg1, string arg2, ..., string n).

I have an array var arr = new string[n] { ..... }

How can I convert array items to be passed fo function f without the need to write

f(arr[0], arr[1], ..., arr[n-1])?

Marco
  • 56,740
  • 14
  • 129
  • 152
  • You should be able to use reflection for this for example – Esailija Mar 06 '13 at 12:55
  • How is N specified? I mean, it can't be dynamic as you have a function with N parameters? – ken2k Mar 06 '13 at 12:56
  • @Esailija: well, I could, but writing a function using Reflection is harder than write parameters manually. Naturally if they are a few (I have 20) – Marco Mar 06 '13 at 12:57
  • @Marco well you can define your own function that takes `params`, and uses reflection to pass them. – Esailija Mar 06 '13 at 12:58

3 Answers3

3

You may think about using a reflection.

In that case, on invoke ou have a possibility to pass an array of arguments:

methodInfo.Invoke(instanceOfClass, parametersArray)

Can look on example:

Reflection: How to Invoke Method with parameters

Keep atention on fact that in this case according to the MethodBase.Invoke documentation you have an object[] array, so you have to deal with boxing/unboxing, don't know if all this effort really worths, just simply passing arguments, in ordinary way.

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I didn't think about this, you're correct. I'll take a look at it in a few hours and be back to you. Thanks! – Marco Mar 06 '13 at 13:01
  • In my specific case probably it doesn't worth (params are 20), but just to have an idea if I need something that takes longer. My concern is readability of the code: writing down `arr[0],....,arr[19]` is not funny nor maintainable... What do you think? – Marco Mar 06 '13 at 13:03
  • @Marco: well, if the only conern is the *readability* and *scallability* it worths to check out this solution. In other cases, just keep ordinar way of doing stuff. – Tigran Mar 06 '13 at 13:05
  • @Marco Honestly, the size of the code isn't the first thing to think about for the maintainability. The complexity and the ability to generate error on compilation if you change your code is much more important IMO. I would go for the 20 parameters as Michel Keijzers said, as reflection removes strongly typing and adds complexity. – ken2k Mar 06 '13 at 13:07
2

You probably can use reflection but I think that would be even more ugly than the 20 parameters written fully.

If you only need it on one location, I would say just call it with 20 parameters. If you need it on multiple places, make a function with 20 parameters in your own program and call that function within your own program everywhere you need it.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

using reflection such as MethodInfo would be one of your choice.

[1] Get the method f's info (pseudo code):

 MethodInfo[] methodInfos = typeof (TheClassWhereFuncfExists).GetMethods(BindingFlags.Public...);

[2] Using linq to get f's method info

MethodInfo fmi= methodInfos.Where(item=> item.Name=="f").FirstOrDefault();

[3] once you get the fmi, you can the programmatically call it:

fmi.Invoke(...)

Pseudo code only, not thoroughly tested.

David
  • 15,894
  • 22
  • 55
  • 66