-2

I have an object array which all has been filled with specific command arguments (of type string):

object[] args = {"@all", "1", "true"};

And I have this method:

public void Method(string client, int num, bool type)<br/>

I want to pass the arguments specified in the object array to the method. How can I convert each object array element to the specific types expected by the method?

Note: The method parameter types ARE NOT FIXED, so I need to convert the items in the object array to different types that are unknown at compile time.

Line 49 format error: https://pastebin.com/bQLsbs59

user6346643
  • 603
  • 4
  • 11
  • This is the purpose of [Casting variables](https://stackoverflow.com/questions/972636/casting-a-variable-using-a-type-variable) – Arthur Attout Jun 08 '18 at 17:21
  • 3
    Sounds like a possible use case for reflection; though the whole thing smells like an XY problem – BradleyDotNET Jun 08 '18 at 17:22
  • Are you ultimately just trying to invoke a function by using a list of objects as the functions arguments? – cwharris Jun 08 '18 at 17:24
  • you can use `MethodInfo.Invoke` from the Reflection namespace. it comes with a performance penalty, but takes an `object[]` so you could pass your parameters directly. The requirement for argument order and overload selections apply just like they would in compile-time call overload resolution. [documented here](https://msdn.microsoft.com/en-us/library/a89hcwhh(v=vs.110).aspx) – Cee McSharpface Jun 08 '18 at 17:26
  • @dlatikay ikr, I am using MethodInfo.Invoke to invoke method. now the problem is that all of my object elements' type are string data type because all of those is apart of string. All I want is those type of element can be automatically convert to Method parameter type. – user6346643 Jun 08 '18 at 17:30
  • Why is args not string[]? You cannot parse on Object to Int. At best you would call Object.ToString() and hope it parses. What problem are you trying to solve? – paparazzo Jun 08 '18 at 17:31
  • Look at line 49: https://pastebin.com/bQLsbs59 I want each type of element of object array can be automatically convert to MethodInfo's parameter type – user6346643 Jun 08 '18 at 17:36

1 Answers1

1

You can use MethodInfo to get the parameters for a method. Then with the ParameterInfo you can get the type of that parameter. Then the final step is to use Convert to change the type of each string to the correct type.

object[] args = { "@all", "1", "true" };

MethodInfo methodInfo = typeof(Form1).GetMethod("Method");
ParameterInfo[] paramInfos = methodInfo.GetParameters();

// Should check that args.Length == paramInfos.Length.

// The arguments converted to their correct types will go in here.
object[] convertedArgs = new object[args.Length];

for (int i = 0; i < args.Length; i++)
{
  // Should do a try/catch here in-case the string can't be
  // converted to the parameter type. i.e. trying to convert
  // "abc" to an int.

  // Change each string to the appropriate type.
  convertedArgs[i] = Convert.ChangeType(args[i],
    paramInfos[i].ParameterType);
}

// Finally, we can call the method with the arguments that have
// been converted to the correct types.
methodInfo.Invoke(this, convertedArgs);
gunnerone
  • 3,566
  • 2
  • 14
  • 18