C#2.0:MethodBase.GetCurrentMethod()可以返回null吗?

时间:2009-11-25 19:15:48

标签: null c#-2.0

我正在追踪NullReferenceException并且缺少the official documentation

这是C#2.0代码。

1 个答案:

答案 0 :(得分:2)

看看Reflector,看起来它可以:

[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());
}