views:

104

answers:

2

I'd like to call methods of a class dynamically with parameter values that are "parsed" from a string input.

For example: I'd like to call the following program with these commands:

c:>myprog.exe MethodA System.Int32 777
c:>myprog.exe MethodA System.float 23.17
c:>myprog.exe MethodB System.Int32& 777
c:>myprog.exe MethodC System.Int32 777 System.String ThisCanBeDone

static void Main(string[] args)
{
     ClassA aa = new ClassA();
     System.Type[] types = new Type[args.Length / 2];
     object[] ParamArray = new object[types.Length];

     for (int i=0; i < types.Length; i++)
       {
          types[i] = System.Type.GetType(args[i*2 + 1]);
      // LINE_X: this will obviously  cause runtime error invalid type/casting
          ParamArray[i] = args[i*2 + 2];  

     MethodInfo callInfo = aa.GetType().GetMethod(args[0],types);
     callInfo.Invoke(aa, ParamArray);
}

// In a non-changeable classlib:

public class ClassA { public void MethodA(int i) { Console.Write(i.ToString()); }

    public void MethodA(float f) { Console.Write(f.ToString()); }

    public void MethodB(ref int i) { Console.Write(i.ToString()); i++; }

    public void MethodC(int i, string s) { Console.Write(s + i.ToString()); }

    public void MethodA(object o) { Console.Write("Argg! Type Trapped!"); }
}

"LINE_X" in the above code is the sticky part. For one, I have no idea how to assign value to a int or a ref int parameter even after I create it using Activator.CreatInstance or something else. The typeConverter does come to mind, but then that requires an explicit compile type casting as well.

Am I looking at CLR with JavaScript glasses or there is way to do this?

+2  A: 

Try this:

void Main(string[] args)
{
 ClassA a = new ClassA();
 System.Type[] types = new Type[(args.Length -1) / 2];
 object[] ParamArray = new object[types.Length];

 for (int i=0; i < types.Length; i++)
 {
      if(args[i*2 + 1].EndsWith("&"))
    {
        var type = System.Type.GetType(args[i*2 + 1].Substring(0,args[i*2 +1].Length - 1)); 
        ParamArray[i] = Convert.ChangeType(args[i*2 + 2],type);
        types[i] = System.Type.GetType(args[i*2 + 1]);
    }
    else
    {
        types[i] = System.Type.GetType(args[i*2 + 1]);
        ParamArray[i] = Convert.ChangeType(args[i*2 + 2],types[i]);
    }      
 }

 MethodInfo callInfo = typeof(ClassA).GetMethod(args[0],types);
 callInfo.Invoke(a, ParamArray);    

}

EDIT: This should take care of the reference parameters

Stephan
Thanks. This works for the int type, but not the "ref int" as that can't be converted (apparently).
MandoMando
I used your answer with: ChangeType(args[i * 2 + 2],Type.GetType(types[i].ToString().Replace(" that did it, but not sure about "out" params now :)
MandoMando
+1  A: 

Youll need to strip off the & to convert it if you want the ref to work:

  static void Main(string[] args)
  {
     ClassA a = new ClassA();
     int half_arg_length = args.Length / 2;
     System.Type[] param_types = new Type[half_arg_length];
     object[] param_values = new object[half_arg_length];

     for (int i = 0; i < half_arg_length; i++)
     {
        string string_type = args[i * 2 + 1];
        param_types[i] = System.Type.GetType(string_type);
        Type convert_type = System.Type.GetType(string_type.TrimEnd('&'));
        param_values[i] = Convert.ChangeType(args[i * 2 + 2], convert_type);
     }
     MethodInfo callInfo = typeof(ClassA).GetMethod(args[0], param_types);
     object res = callInfo.Invoke(a, param_values);
  }
SwDevMan81