如何获得C ++中的当前行号,函数名等? [英] How can I get current line number, function name, etc. like in C++?

查看:141
本文介绍了如何获得C ++中的当前行号,函数名等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在我的C#

应用程序中确定我当前的行,文件和函数。我知道C ++有__LINE __,__ FUNCTION__和__FILE___

宏来获取它,但我找不到C#等价物。有什么想法吗?


-

------------------------- ----------

Ken Varn

高级软件工程师

Diebold Inc.
va *** @ diebold.com

----------------- ------------------

I want to be able to determine my current line, file, and function in my C#
application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___
macros for getting this, but I cannot find a C# equivalent. Any ideas?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------

推荐答案

我想你至少可以从中得到一些StackFrame类。


Jonathan Schafer


2003年7月29日星期二16:02:50 -0400,Ken Varn < va *** @ diebold.com>

写道:
I think you can get at least some of this from the StackFrame class.

Jonathan Schafer

On Tue, 29 Jul 2003 16:02:50 -0400, "Ken Varn" <va***@diebold.com>
wrote:
我希望能够确定我当前的行,文件和功能C#
申请。我知道C ++有__LINE __,__ FUNCTION__和__FILE___
宏来获取它,但我找不到C#等价物。有什么想法吗?
I want to be able to determine my current line, file, and function in my C#
application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___
macros for getting this, but I cannot find a C# equivalent. Any ideas?








嗨肯,


In C#,您可以使用System.Diagnostics.StackTrace类来获取代码?

行号,函数名,文件名和其他信息。

我的示例代码如下所示:


使用System;

使用System.Diagnostics;


名称空间getline

{

class Class1

{

[STAThread]

static void Main(string [] args)

{

StackTrace st = new StackTrace(0,true);


StackFrame sf = new StackFrame();

sf = st.GetFrame(0);

Console.WriteLine(" FileName:{0}",sf.GetFileName());

Console.WriteLine("行号:

{0}",sf.GetFileLineNumber());

Console.WriteLine(" Function Name:

{0}",sf.GetMethod());


Console.Read();

}

}

}


希望这会有所帮助。


Jeffrey Tan

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。


--------------------

|回复:Ken Varn < va *** @ diebold.com>

|来自:Ken Varn < va *** @ diebold.com>

|主题:我如何获得当前行号,函数名称等,如

C ++?

|日期:2003年7月29日星期二16:02:50 -0400

|行数:13

|组织:Diebold Inc.

| X-Priority:3

| X-MSMail-Priority:正常

| X-Newsreader:Microsoft Outlook Express 6.00.2800.1158

| X-MimeOLE:由Microsoft MimeOLE V6.00.2800.1165制作

|消息ID:< #w ************** @ TK2MSFTNGP11.phx.gbl>

|新闻组:microsoft.public.dotnet.languages.csharp

| NNTP-Posting-Host:204.151.249.23

|路径:cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl

|外翻:cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172768

| X-Tomcat-NG:microsoft.public.dotnet.languages.csharp

|

|我想能够确定我当前的行,文件和功能在我的

C#

|应用。我知道C ++有__LINE __,__ FUNCTION__和

__FILE___

|得到这个的宏,但我找不到C#等价物。有什么想法吗?

|

| -

| -----------------------------------

| Ken Varn

|高级软件工程师

| Diebold Inc.

| va***@diebold.com

| -----------------------------------

|

|

|


Hi Ken,

In C#, you can use System.Diagnostics.StackTrace class to get the code?s
line number, function name, filename and other information.
My sample code was listed below:

using System;
using System.Diagnostics ;

namespace getline
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StackTrace st=new StackTrace (0,true);

StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName ());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name:
{0}",sf.GetMethod ());

Console.Read ();
}
}
}

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Ken Varn" <va***@diebold.com>
| From: "Ken Varn" <va***@diebold.com>
| Subject: How can I get current line number, function name, etc. like in
C++?
| Date: Tue, 29 Jul 2003 16:02:50 -0400
| Lines: 13
| Organization: Diebold Inc.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#w**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 204.151.249.23
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172768
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I want to be able to determine my current line, file, and function in my
C#
| application. I know that C++ has the __LINE__, __FUNCTION__, and
__FILE___
| macros for getting this, but I cannot find a C# equivalent. Any ideas?
|
| --
| -----------------------------------
| Ken Varn
| Senior Software Engineer
| Diebold Inc.
| va***@diebold.com
| -----------------------------------
|
|
|


我已经使用了堆栈框架,但唯一的问题是我是/>
考虑使用.net代码混淆器,我担心这个

信息将无法作为运行时数据读取。如果可能的话,我希望能够在编译时获得它。


-

----- ------------------------------

Ken Varn

高级软件工程师

Diebold Inc.
va***@diebold.com

-----------------------------------

Jonathan Schafer ; < jschafer@*NOSPAM*brierley.a.b.c.com>在消息中写道

news:h3 ******************************** @ 4ax.com ...
I have used the stack frame for this, but the only problem is that I am
contemplating using a .net code obfuscator and I am concerned that this
information will not be readable as runtime data. I would prefer to be able
to get it at compile time if possible.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
va***@diebold.com
-----------------------------------
"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:h3********************************@4ax.com...
我认为你可以从StackFrame课程中获得至少一部分内容。

Jonathan Schafer

2003年7月29日星期二16:02:50 - 0400,Ken Varn < va *** @ diebold.com>
写道:
I think you can get at least some of this from the StackFrame class.

Jonathan Schafer

On Tue, 29 Jul 2003 16:02:50 -0400, "Ken Varn" <va***@diebold.com>
wrote:
我希望能够在
中确定我当前的行,文件和函数C#应用程序。我知道C ++有__LINE __,__ FUNCTION__和
__FILE___macros来获取它,但我找不到C#等价物。有什么想法?
I want to be able to determine my current line, file, and function in my C#application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___macros for getting this, but I cannot find a C# equivalent. Any ideas?



这篇关于如何获得C ++中的当前行号,函数名等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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