代码之家  ›  专栏  ›  技术社区  ›  emptyset

C 2.0:methodBase.getcurrentMethod()能否返回空值?

  •  1
  • emptyset  · 技术社区  · 16 年前

    我正在跟踪一个NullReferenceException和 the official documentation 缺乏。

    这是C 2.0代码。

    1 回复  |  直到 16 年前
        1
  •  2
  •   John Rasch    16 年前

    看着反射镜,它看起来可以:

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static MethodBase GetCurrentMethod()
    {
        StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;
        return RuntimeMethodInfo.InternalGetCurrentMethod(ref lookForMyCaller);
    }
    

    InternalGetCurrentMethod 看起来像:

    internal static MethodBase InternalGetCurrentMethod(ref StackCrawlMark stackMark)
    {
        RuntimeMethodHandle currentMethod = RuntimeMethodHandle.GetCurrentMethod(ref stackMark);
        if (currentMethod.IsNullHandle())
        {
            return null;
        }
        return RuntimeType.GetMethodBase(currentMethod.GetTypicalMethodDefinition());
    }
    
    推荐文章