c# - Parse string to valid array -


i need parse command line arguments specific datatypes defined method signature. so, input arguments in main method, have of possible cases follows:

[11, 23, 32] [1,24.254,3] [ 124 ,2, 3.0125 ] [1, 25, 03.1], 1 , "a", 42, "aac", ["aaa", "bbb", "ccc"] 

with possible combinations of stated cases. there, of course can errors unbalanced parentheses, , or .. so, can validate unbalanced parentheses, don't know how parse , validate other stuff. again, elements must separated , , know necessary datatype of each element (if method public void mymethod(string[], int a, string b, double c) can parse accordingly).

does has idea of how can achieved, written solution or else?

string[] input = //cli arguments separated ,  // "yourtype" type holds method methodinfo targetmethod = typeof(yourtype).getmethod("mymethod");  // yourmethod's parameters parameterinfo[] parameters = targetmethod.getparameters();  // contain converted values object[] arguments = new object[input.length];  for(int = 0; < input.length; i++) {     type paramtype = parameters[i].parametertype;     if (paramtype.isarray)     {         string[] arrayvalues = input[i]                                .replace("[", string.empty)                                .replace("]", string.empty)                                .split(',');         arguments[i] = arrayvalues                     .select(v => convert.changetype(v, paramtype.getelementtype())                     .toarray();     }     else         arguments[i] = convert.changetype(input[i], paramtype); } 

you'd invoke method converted values.

if mymethod static:

targetmethod.invoke(null, arguments); 

if mymethod instance method

yourtype instance = // instantiate new object targetmethod.invoke(instance, arguments); 

Comments