.net - 检索调用堆栈中事件方法之前的方法的 MethodInfo?

标签 .net reflection

我已经为日志库创建了包装器,我想在其中记录抛出异常的外部方法的参数:

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

...

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

如您所见,我想获取有关错误方法级别的 MethodA 信息。我会非常乐意使用:

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

因为它拥有一切,但我只是阅读了多篇关于性能不佳的 SO 帖子。在我看来,这可能不是最好的方法(另一方面,我们正在谈论异常处理——此时它已经不太重要了)。但是,我找不到使用反射来解决这个问题的传统方法。也许有人可以启发我?

或者您仍然会使用 StackFrame 解决方案? 它是对超高性能要求相当低的 asp.net 应用程序。

编辑1: 也许我不太确定我想写什么。我正在使用 StackFrame 解决方案,即使它最多需要 20 毫秒才能获得一个帧,但我真的很好奇如何通过反射获得类似的结果。

最佳答案

您可能会发现我的这个实用方法很有用,因为它还解决了您可能会遇到的另一个问题:当您想从构造函数中获取当前堆栈帧时,事情有点奇怪。

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

(注意:Collections.Util.NewListReadonly() 是我的一个静态实用方法,它创建一个只读列表并返回其 IList<T> 接口(interface)。)

我不知道 new Diagnostics.StackTrace() 的性能: 我猜它和使用反射一样慢,(它可能算作反射)但可能比抛出异常快一点。

不过,我会让您对参数部分感到失望:我没有找到在运行时获取方法参数内容的方法。因此,您希望得到的只是参数的类型和名称,而不是它们的值。如果您找到方法,请告诉我。

关于.net - 检索调用堆栈中事件方法之前的方法的 MethodInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8331466/

相关文章:

c# - 如何通过反射调用带有枚举(enum)参数的方法?

c# - 为什么不使用 HashSet<T> 类来实现 Enumerable.Distinct

c# - 为框架类型注入(inject) TypeConverter 是否安全?

Scala:获取内部类型的 TypeTag

c# - 事件设计指南

c# - 在 C#.NET 中动态转换为类型

C# 反射 - 在运行时确定依赖项的位置

c# - 为什么在所有初始订阅者断开连接后 RefCount 不工作?

.net - 为什么 ValidateAntiForgeryTokenAttribute 允许匿名 token ?

c# - foreach 比在 .cs 上构建字符串慢?