相关疑难解决方法(0)

我可以从当前执行的函数中以程序方式获取参数名称/值吗?

我想做这样的事情:

public MyFunction(int integerParameter, string stringParameter){
    //Do this:
    LogParameters();
    //Instead of this:
    //Log.Debug("integerParameter: " + integerParameter + 
    //          ", stringParameter: " + stringParameter);

}

public LogParameters(){
    //Look up 1 level in the call stack (if possible),
    //Programmatically loop through the function's parameters/values
    //and log them to a file (with the function name as well).
    //If I can pass a MethodInfo instead of analyzing the call stack, great.
}
Run Code Online (Sandbox Code Playgroud)

我甚至不确定我想要做什么是可能的,但能够在运行时自动将参数名称/值输出到文件而不显式编写代码来记录它们将是非常好的.

可能吗?

.net c# reflection parameters logging

40
推荐指数
3
解决办法
3万
查看次数

是否可以在.NET中调用堆栈中的每个帧获取参数值

我在谈论托管的.NET代码.如果我们运行任何程序并将VS附加到它,我们可以看到调用堆栈中每个方法的参数值.我想创建一个日志记录解决方案,它将记录调用堆栈中每个方法的所有参数值.实际上我需要这个信息,以防发生异常.

我知道可以通过分析API来实现.但我想知道只有托管代码才有可能吗?

更新:好的,可能使用纯.NET,这是不可能的.然后可能有某种非托管代码......重点是从应用程序本身内部执行此操作.在异常的情况下,应用程序可以调用某个库(可能是非托管的),它返回有关调用堆栈中方法值的信息.只是想一想......

.net clr diagnostics

11
推荐指数
1
解决办法
2348
查看次数

如何在数组中获取方法参数?

想象一下,你在这个课程中得到了这个方法:

float Do(int a_,string b_){}
Run Code Online (Sandbox Code Playgroud)

我正在尝试做这样的事情:

float Do(int a_, string b_)
{
  var params = GetParamsListOfCurrentMethod(); //params is an array that contains (a_ and b_)
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

我为什么要这样做?

想象一下,你有一个接口:

public Interface ITrucMuch
{
 float Do(int a_,string b_);
 // And much more fct
}
Run Code Online (Sandbox Code Playgroud)

还有很多实现该接口的类

还有一个特殊的类,它也实现了接口:

public class MasterTrucMuch : ITrucMuch
{
  public floatDo(int a_, string b_) 
  {
    ITrucMuch tm = Factory.GetOptimizedTrucMuch(); // This'll return an optimized trucMuch based on some state
    if(tm != null)
    {
      return tm.Do(a_,b_);
    }
    else
    { …
Run Code Online (Sandbox Code Playgroud)

c#

6
推荐指数
2
解决办法
9028
查看次数

标签 统计

.net ×2

c# ×2

clr ×1

diagnostics ×1

logging ×1

parameters ×1

reflection ×1