是否有内置的方法来确定递归级别? [英] Is there a built in way to determine the recursion level?

查看:113
本文介绍了是否有内置的方法来确定递归级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Server中有@@ NESTLEVEL,它在递归函数调用中返回当前的

级别数。是否有一个C#等价物来获得递归方法调用的当前级别

?我希望能够在任何给定的时间点告诉我多少级别。


谢谢


JIM

In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM

推荐答案



" james" <无**** @ hypercon.net>在消息中写道

新闻:uT ************** @ TK2MSFTNGP15.phx.gbl ...

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
在SQL Server中有@@ NESTLEVEL,它在递归函数调用中返回当前的
级别数。是否有一个C#等效来获得递归方法调用的当前级别?我希望能够告诉我在任何给定点上有多少级别。
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.




不是真的,但你可以访问这个电话stack:


StackTrace trace = new StackTrace(true);

int stackFrames = trace.FrameCount;


这与@@ NESTLEVEL不一样,但每个方法都会增加

来电。


-

Mickey Williams

作者,Visual C#.NET Core Ref,MS Press
www.neudesic.com
www.servergeek.com





没有AFAIK,您可以尝试使用StackTrace并浏览StackFrames

计数。


或者您可以使用计数器实现它并将其作为参数传递。


欢呼,

-

Ignacio Machi n,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


" james" <无**** @ hypercon.net>在消息中写道

新闻:uT ************** @ TK2MSFTNGP15.phx.gbl ...
Hi,

NO AFAIK, you could try to use StackTrace and go over the StackFrames
counting.

Or you could implement it using a counter and passing it as a parameter.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
在SQL Server中有@@ NESTLEVEL,它在递归函数调用中返回当前的
级别数。是否有一个C#等效来获得递归方法调用的当前级别?我希望能够告诉我在任何给定点上有多少级别。

感谢

JIM
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM



我通常只是通过添加一个类变量计数器来编写代码。例如


private int m_iMRGLevel;

private void FnCallingMRF()

{

m_iMRGLevel = 0 ;

MyRecursiveFunction();

}

private void MyRecursiveFunction()

{

m_iMRFLevel ++;

//更多代码

if(someCondition)

MyRecursiveFunction();

}


当然,你也可以使用这样的ref参数:

private void FnCallingMFR()

{

int iLevel = 0;

MyRecursiveFunction(ref iLevel);

}

private void MyRecursiveFunction(ref int level)

{

level ++;

if(someCondition)

MyRecursiveFunction(ref level);

}


" james" <无**** @ hypercon.net>在消息中写道

新闻:uT ************** @ TK2MSFTNGP15.phx.gbl ...
I usually just code it by adding a class variable counter. E.g.

private int m_iMRGLevel;
private void FnCallingMRF()
{
m_iMRGLevel = 0;
MyRecursiveFunction();
}
private void MyRecursiveFunction()
{
m_iMRFLevel++;
// More code
if( someCondition )
MyRecursiveFunction();
}

Of course, you can also use a ref parameter like this:
private void FnCallingMFR()
{
int iLevel = 0;
MyRecursiveFunction( ref iLevel );
}
private void MyRecursiveFunction( ref int level )
{
level++;
if( someCondition )
MyRecursiveFunction( ref level );
}

"james" <no****@hypercon.net> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
在SQL Server中有@@ NESTLEVEL,它在递归函数调用中返回当前的
级别数。是否有一个C#等效来获得递归方法调用的当前级别?我希望能够告诉我在任何给定点上有多少级别。

感谢

JIM
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM



这篇关于是否有内置的方法来确定递归级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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