平々毎々(アーカイブ)

はてなダイアリーのアーカイブです。

ByRef parameter and C#

class P
{
  public static void Method(out string arg)
  {
    arg = "out";
    throw new Exception();
  }
}

if you call P.Method() directly, Exception will be thrown and you can get a string "out".

if you do it through reflection:

object[] args = new object[1];
MethodInfo mi = typeof(P).GetMethod("Method");
mi.Invoke(null, args);

TargetInvocationException will be thrown and you cannot get a string "out". args[0] will be still null.

so, is it possible to call P.Method() dynamically and get "out" value in C#?