How to use reflection to call a method and pass parameters whose types are unknown at compile time?

I would like to call class methods dynamically with parameters that are "parsed" from string input.

For example: I would call the following command the following 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 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" . -, , int ref int Activator.CreatInstance - . , , .

CLR JavaScript ?

+3
2

:

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);    

}

:

+3

, , ref :

  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);
  }
+1

Source: https://habr.com/ru/post/1745352/


All Articles