小编mat*_*llo的帖子

如何从抛出异常的方法中通过out/ref参数获取值?

此代码输出"out value".

class P
{
  public static void Main()
  {
    string arg = null;
    try
    {
      Method(out arg);
    }
    catch
    {
    }
    Console.WriteLine(arg);
  }
  public static void Method(out string arg)
  {
    arg = "out value";
    throw new Exception();
  }
}
Run Code Online (Sandbox Code Playgroud)

但这一个没有.

class P
{
  public static void Main()
  {
    object[] args = new object[1];
    MethodInfo mi = typeof(P).GetMethod("Method");
    try
    {
      mi.Invoke(null, args);
    }
    catch
    {
    }
    Console.WriteLine(args[0]);
  }
  public static void Method(out string arg)
  {
    arg = "out value";
    throw …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection exception byref

12
推荐指数
1
解决办法
1576
查看次数

标签 统计

.net ×1

byref ×1

c# ×1

exception ×1

reflection ×1