检索这是以前活跃的一个调用堆栈上的方法的MethodInfo? [英] Retrieve MethodInfo of the method that is before the active one on the calling stack?

查看:201
本文介绍了检索这是以前活跃的一个调用堆栈上的方法的MethodInfo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了包装记录库里面,而我要记录的外部方法抛出的异常的参数:

I've created wrapper for logging library inside of which I want to log parameters of the outer method that thrown exception:

public void MethodA(string param1, int param2)
{
  try
  {
    throw new Exception();
  }
  catch(Exception ex)
  {
    Logger.Error(ex);
  }
}

...

public static void Error(Exception ex)
{
...
}

正如你可以看到,我想得治法信息的错误方法的水平。我会更乐意只使用方法:

As you can see I would like to get MethodA informations on the Error method level. I would be more than happy just to use:

ParameterInfo[] pi = new StackFrame(1).GetMethod().GetParameters();

由于它的一切,但我刚刚看了一下性能差多SO职位。在我看来,这也许不是那么最好的办法(在另一方面,我们正在谈论的异常处理 - 在这个时间点上它已经不是太重要了)。但是,我找不到传统的方法使用反射来解决这个问题。也许有人能告诉我吗?

because it has everything, but I just read multiple SO posts about poor performance. Seems to me that maybe it's not the best way then (on the other hand, we're talking about exception handling - at this point of time it's already not too important). However, I cannot find traditional approach using Reflection to solve this. Perhaps someone can enlighten me?

或者,也许你仍然用的StackFrame的解决方案去? 这是asp.net应用程序与超性能相当小的需求。

Or maybe you would still go with StackFrame solution ? It's asp.net application with rather small demands for ultra performance.

EDIT1: 也许我不敢肯定我想写。我会用的StackFrame解决方案,即使它可能需要长达20毫秒得到一个框架,但我真的很好奇如何让similiar结果与反思。

Maybe I was not so sure what I want to write. I AM going with StackFrame solution even if it can take up to 20 ms to get a frame but I am really curious how to get similiar result with reflection.

推荐答案

您可能会发现矿用这个工具方法,因为它也解决了另一个问题,你可能会遇到:当你想要的东西是有点怪从构造函数中获取当前栈帧。

You might find this utility method of mine useful, since it also solves another problem which you are likely to come across: things are a bit weird when you want to get the current stack frame from within a constructor.

using Collections = MikeNakis.Abstract.Collections;
using Diagnostics = System.Diagnostics;
using Generic = System.Collections.Generic;
using Reflection = System.Reflection;
[...]
    ///<summary>Collect stack frames</summary>
    ///<param name="frames_to_skip">Number of frames to skip (usually 1)</param>
    ///<param name="type">When invoking from within a constructor, specify the type of the containing class so as to
    ///skip any other constructors.</param>
    public static Generic.IList<Diagnostics.StackFrame> CollectStackframes( int frames_to_skip, System.Type type = null )
    {
        var frames = new Diagnostics.StackTrace( frames_to_skip + 1, fNeedFileInfo:true ).GetFrames();
        int i = 0;
        if( type != null )
        {
            for( ;  i < frames.Length;  i++ )
            {
                Reflection.MethodBase method = frames[i].GetMethod();
                if( !method.IsConstructor )
                    break;
                //Does not work: if( method.DeclaringType == type )
                //Does not work: if( Equals( method.DeclaringType, type ) )
                //Does not work: if( Equals( method.DeclaringType.TypeHandle, type.TypeHandle ) )
                if( Equals( method.DeclaringType.GUID, type.GUID ) )
                {
                    i++;
                    break;
                }
            }
        }
        var list_of_frame = new Generic.List<Diagnostics.StackFrame>( frames.Length - i );
        for( ;  i < frames.Length;  i++ )
            list_of_frame.Add( frames[i] );
        return Collections.Util.NewListReadonly( list_of_frame );
    }

(注: Col​​lections.Util.NewListReadonly()是我的静态工具方法,它创建了一个只读列表,并返回其的IList&LT ; T&GT; 接口)

(Note: Collections.Util.NewListReadonly() is a static utility method of mine which creates a read-only list and returns its IList<T> interface.)

我不知道关于新Diagnostics.StackTrace()业绩:我猜想,这是因为使用反射慢,(它可能算作反思, ),但可能有点快于抛出异常。

I do not know about the performance of new Diagnostics.StackTrace(): I would guess that it is as slow as using reflection, (it probably counts as reflection,) but probably a bit faster than throwing an exception.

我会令你失望有关参数的一部分,虽然我没有发现任何方式来获得的参数的内容在运行时的方法。所以,尽你所能希望得到的类型和参数的名称,而不是它们的值。如果你找到一种方法,请让我知道。

I will disappoint you about the parameters part though: I have found no way to obtain the contents of parameters to methods at runtime. So, all that you can hope to get is the types and names of the parameters, but not their values. If you find a way, please let me know.

这篇关于检索这是以前活跃的一个调用堆栈上的方法的MethodInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆