StackFrame 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供關於一個 StackFrame的資訊,代表當前執行緒呼叫堆疊上的函式呼叫。
public ref class StackFrame sealed
public ref class StackFrame
public sealed class StackFrame
[System.Serializable]
public class StackFrame
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StackFrame
public class StackFrame
type StackFrame = class
[<System.Serializable>]
type StackFrame = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StackFrame = class
Public NotInheritable Class StackFrame
Public Class StackFrame
- 繼承
-
StackFrame
- 屬性
範例
以下範例展示了如何利用類別 StackFrame 提供堆疊追蹤的堆疊框架資訊。
using System;
using System.Diagnostics;
namespace StackFrameExample
{
class Program
{
static void Main(string[] args)
{
try
{
Method1();
}
catch (Exception e)
{
StackTrace st = new StackTrace();
StackTrace st1 = new StackTrace(new StackFrame(true));
Console.WriteLine(" Stack trace for Main: {0}",
st1.ToString());
Console.WriteLine(st.ToString());
}
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
}
private static void Method1()
{
try
{
Method2(4);
}
catch (Exception e)
{
StackTrace st = new StackTrace();
StackTrace st1 = new StackTrace(new StackFrame(true));
Console.WriteLine(" Stack trace for Method1: {0}",
st1.ToString());
Console.WriteLine(st.ToString());
// Build a stack trace for the next frame.
StackTrace st2 = new StackTrace(new StackFrame(1, true));
Console.WriteLine(" Stack trace for next level frame: {0}",
st2.ToString());
throw e;
}
}
private static void Method2( int count)
{
try
{
if (count < 5)
throw new ArgumentException("count too large", "count");
}
catch (Exception e)
{
StackTrace st = new StackTrace();
StackTrace st1 = new StackTrace(new StackFrame(2,true));
Console.WriteLine(" Stack trace for Method2: {0}",
st1.ToString());
Console.WriteLine(st.ToString());
throw e;
}
}
}
}
Imports System.Diagnostics
Class Program
Shared Sub Main(ByVal args() As String)
Try
Method1()
Catch e As Exception
Dim st As New StackTrace()
Dim st1 As New StackTrace(New StackFrame(True))
Console.WriteLine(" Stack trace for Main: {0}", st1.ToString())
Console.WriteLine(st.ToString())
End Try
Console.WriteLine("Press Enter to exit.")
Console.ReadLine()
End Sub
Private Shared Sub Method1()
Try
Method2(4)
Catch e As Exception
Dim st As New StackTrace()
Dim st1 As New StackTrace(New StackFrame(True))
Console.WriteLine(" Stack trace for Method1: {0}", st1.ToString())
Console.WriteLine(st.ToString())
' Build a stack trace for the next frame.
Dim st2 As New StackTrace(New StackFrame(1, True))
Console.WriteLine(" Stack trace for next level frame: {0}", st2.ToString())
Throw e
End Try
End Sub
Private Shared Sub Method2(ByVal count As Integer)
Try
If count < 5 Then
Throw New ArgumentException("count too large", "count")
End If
Catch e As Exception
Dim st As New StackTrace()
Dim st1 As New StackTrace(New StackFrame(2, True))
Console.WriteLine(" Stack trace for Method2: {0}", st1.ToString())
Console.WriteLine(st.ToString())
Throw e
End Try
End Sub
End Class
以下範例展示了類別成員 StackFrame 的使用方式。
using System;
using System.Diagnostics;
using SampleInternal;
namespace SamplePublic
{
// This console application illustrates various uses
// of the StackTrace and StackFrame classes.
class ConsoleApp
{
[STAThread]
static void Main()
{
ClassLevel1 mainClass = new ClassLevel1();
try {
mainClass.InternalMethod();
}
catch (Exception) {
Console.WriteLine(" Main method exception handler");
// Display file and line information, if available.
StackTrace st = new StackTrace(new StackFrame(true));
Console.WriteLine(" Stack trace for current level: {0}",
st.ToString());
Console.WriteLine(" File: {0}",
st.GetFrame(0).GetFileName());
Console.WriteLine(" Line Number: {0}",
st.GetFrame(0).GetFileLineNumber().ToString());
Console.WriteLine();
Console.WriteLine("-------------------------------------------------\n");
}
}
}
}
namespace SampleInternal
{
public class ClassLevel1
{
public void InternalMethod()
{
try
{
ClassLevel2 nestedClass = new ClassLevel2();
nestedClass.Level2Method();
}
catch (Exception e)
{
Console.WriteLine(" InternalMethod exception handler");
// Build a stack trace from one frame, skipping the
// current frame and using the next frame. By
// default, file and line information are not displayed.
StackTrace st = new StackTrace(new StackFrame(1));
Console.WriteLine(" Stack trace for next level frame: {0}",
st.ToString());
Console.WriteLine(" Stack frame for next level: ");
Console.WriteLine(" {0}", st.GetFrame(0).ToString());
Console.WriteLine(" Line Number: {0}",
st.GetFrame(0).GetFileLineNumber().ToString());
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
}
public class ClassLevel2
{
public void Level2Method()
{
try
{
ClassLevel3 nestedClass = new ClassLevel3();
nestedClass.Level3Method();
}
catch (Exception e)
{
Console.WriteLine(" Level2Method exception handler");
// Display the full call stack at this level.
StackTrace st1 = new StackTrace(true);
Console.WriteLine(" Stack trace for this level: {0}",
st1.ToString());
// Build a stack trace from one frame, skipping the current
// frame and using the next frame.
StackTrace st2 = new StackTrace(new StackFrame(1, true));
Console.WriteLine(" Stack trace built with next level frame: {0}",
st2.ToString());
// Build a stack trace skipping the current frame, and
// including all the other frames.
StackTrace st3 = new StackTrace(1, true);
Console.WriteLine(" Stack trace built from the next level up: {0}",
st3.ToString());
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
}
public class ClassLevel3
{
public void Level3Method()
{
try
{
ClassLevel4 nestedClass = new ClassLevel4();
nestedClass.Level4Method();
}
catch (Exception e)
{
Console.WriteLine(" Level3Method exception handler");
// Build a stack trace from a dummy stack frame.
// Explicitly specify the source file name and
// line number.
StackTrace st = new StackTrace(new StackFrame("source.cs", 60));
Console.WriteLine(" Stack trace with dummy stack frame: {0}",
st.ToString());
for(int i =0; i< st.FrameCount; i++ )
{
// Display the stack frame properties.
StackFrame sf = st.GetFrame(i);
Console.WriteLine(" File: {0}", sf.GetFileName());
Console.WriteLine(" Line Number: {0}",
sf.GetFileLineNumber());
// Note that the column number defaults to zero
// when not initialized.
Console.WriteLine(" Column Number: {0}",
sf.GetFileColumnNumber());
if (sf.GetILOffset() != StackFrame.OFFSET_UNKNOWN)
{
Console.WriteLine(" Intermediate Language Offset: {0}",
sf.GetILOffset());
}
if (sf.GetNativeOffset() != StackFrame.OFFSET_UNKNOWN)
{
Console.WriteLine(" Native Offset: {0}",
sf.GetNativeOffset());
}
}
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
}
public class ClassLevel4
{
public void Level4Method()
{
try
{
ClassLevel5 nestedClass = new ClassLevel5();
nestedClass.Level5Method();
}
catch (Exception e)
{
Console.WriteLine(" Level4Method exception handler");
// Build a stack trace from a dummy stack frame.
// Explicitly specify the source file name, line number
// and column number.
StackTrace st = new StackTrace(new StackFrame("source.cs", 79, 24));
Console.WriteLine(" Stack trace with dummy stack frame: {0}",
st.ToString());
// Access the StackFrames explicitly to display the file
// name, line number and column number properties.
// StackTrace.ToString only includes the method name.
for(int i =0; i< st.FrameCount; i++ )
{
StackFrame sf = st.GetFrame(i);
Console.WriteLine(" File: {0}", sf.GetFileName());
Console.WriteLine(" Line Number: {0}",
sf.GetFileLineNumber());
Console.WriteLine(" Column Number: {0}",
sf.GetFileColumnNumber());
}
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
}
public class ClassLevel5
{
public void Level5Method()
{
try
{
ClassLevel6 nestedClass = new ClassLevel6();
nestedClass.Level6Method();
}
catch (Exception e)
{
Console.WriteLine(" Level5Method exception handler");
StackTrace st = new StackTrace();
// Display the most recent function call.
StackFrame sf = st.GetFrame(0);
Console.WriteLine();
Console.WriteLine(" Exception in method: ");
Console.WriteLine(" {0}", sf.GetMethod());
if (st.FrameCount >1)
{
// Display the highest-level function call
// in the trace.
sf = st.GetFrame(st.FrameCount-1);
Console.WriteLine(" Original function call at top of call stack):");
Console.WriteLine(" {0}", sf.GetMethod());
}
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
}
public class ClassLevel6
{
public void Level6Method()
{
throw new Exception("An error occurred in the lowest internal class method.");
}
}
}
Imports System.Diagnostics
Imports SampleInternal
Namespace SamplePublic
' This console application illustrates various uses
' of the StackTrace and StackFrame classes.
Class ConsoleApp
<STAThread()> _
Shared Sub Main()
Dim mainClass As New ClassLevel1
Try
mainClass.InternalMethod()
Catch
Console.WriteLine(" Main method exception handler")
' Display file and line information, if available.
Dim st As New StackTrace(New StackFrame(True))
Console.WriteLine(" Stack trace for current level: {0}", _
st.ToString())
Console.WriteLine(" File: {0}", _
st.GetFrame(0).GetFileName())
Console.WriteLine(" Line Number: {0}", _
st.GetFrame(0).GetFileLineNumber().ToString())
Console.WriteLine()
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
End Try
End Sub
End Class
End Namespace 'StackFramePublic
Namespace SampleInternal
Public Class ClassLevel1
Public Sub InternalMethod()
Try
Dim nestedClass As New ClassLevel2
nestedClass.Level2Method()
Catch e As Exception
Console.WriteLine(" InternalMethod exception handler")
' Build a stack trace from one frame, skipping the
' current frame and using the next frame. By default,
' file and line information are not displayed.
Dim st As New StackTrace(New StackFrame(1))
Console.WriteLine(" Stack trace for next level frame: {0}", _
st.ToString())
Console.WriteLine(" Stack frame for next level: ")
Console.WriteLine(" {0}", st.GetFrame(0).ToString())
Console.WriteLine(" Line Number: {0}", _
st.GetFrame(0).GetFileLineNumber().ToString())
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
End Class
Public Class ClassLevel2
Public Sub Level2Method()
Try
Dim nestedClass As New ClassLevel3
nestedClass.Level3Method()
Catch e As Exception
Console.WriteLine(" Level2Method exception handler")
' Display the full call stack at this level.
Dim st1 As New StackTrace(True)
Console.WriteLine(" Stack trace for this level: {0}", _
st1.ToString())
' Build a stack trace from one frame, skipping the current
' frame and using the next frame.
Dim st2 As New StackTrace(New StackFrame(1, True))
Console.WriteLine(" Stack trace built with next level frame: {0}", _
st2.ToString())
' Build a stack trace skipping the current frame, and
' including all the other frames.
Dim st3 As New StackTrace(1, True)
Console.WriteLine(" Stack trace built from the next level up: {0}", _
st3.ToString())
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
End Class
Public Class ClassLevel3
Public Sub Level3Method()
Try
Dim nestedClass As New ClassLevel4()
nestedClass.Level4Method()
Catch e As Exception
Console.WriteLine(" Level3Method exception handler")
' Build a stack trace from a dummy stack frame.
' Explicitly specify the source file name and line number.
Dim st As New StackTrace(New StackFrame("source.cs", 60))
Console.WriteLine(" Stack trace with dummy stack frame: {0}", _
st.ToString())
Dim i As Integer
For i = 0 To st.FrameCount - 1
' Display the stack frame properties.
Dim sf As StackFrame = st.GetFrame(i)
Console.WriteLine(" File: {0}", sf.GetFileName())
Console.WriteLine(" Line Number: {0}", _
sf.GetFileLineNumber())
' The column number defaults to zero when not initialized.
Console.WriteLine(" Column Number: {0}", _
sf.GetFileColumnNumber())
If sf.GetILOffset <> StackFrame.OFFSET_UNKNOWN
Console.WriteLine(" Intermediate Language Offset: {0}", _
sf.GetILOffset())
End If
If sf.GetNativeOffset <> StackFrame.OFFSET_UNKNOWN
Console.WriteLine(" Native Offset: {0}", _
sf.GetNativeOffset())
End If
Next i
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
End Class
Public Class ClassLevel4
Public Sub Level4Method()
Try
Dim [nestedClass] As New ClassLevel5()
[nestedClass].Level5Method()
Catch e As Exception
Console.WriteLine(" Level4Method exception handler")
' Build a stack trace from a dummy stack frame.
' Explicitly specify the source file name, line number
' and column number.
Dim st As New StackTrace(New StackFrame("source.cs", 79, 24))
Console.WriteLine(" Stack trace with dummy stack frame: {0}", _
st.ToString())
' Access the StackFrames explicitly to display the file
' name, line number and column number properties.
' StackTrace.ToString only includes the method name.
Dim i As Integer
For i = 0 To st.FrameCount - 1
Dim sf As StackFrame = st.GetFrame(i)
Console.WriteLine(" File: {0}", sf.GetFileName())
Console.WriteLine(" Line Number: {0}", _
sf.GetFileLineNumber())
Console.WriteLine(" Column Number: {0}", _
sf.GetFileColumnNumber())
Next i
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
End Class
Public Class ClassLevel5
Public Sub Level5Method()
Try
Dim nestedClass As New ClassLevel6()
nestedClass.Level6Method()
Catch e As Exception
Console.WriteLine(" Level5Method exception handler")
Dim st As New StackTrace()
' Display the most recent function call.
Dim sf As StackFrame = st.GetFrame(0)
Console.WriteLine()
Console.WriteLine(" Exception in method: ")
Console.WriteLine(" {0}", sf.GetMethod())
If st.FrameCount > 1 Then
' Display the highest-level function call in the trace.
sf = st.GetFrame((st.FrameCount - 1))
Console.WriteLine(" Original function call at top of call stack):")
Console.WriteLine(" {0}", sf.GetMethod())
End If
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
End Class
Public Class ClassLevel6
Public Sub Level6Method()
Throw New Exception("An error occurred in the lowest internal class method.")
End Sub
End Class
End Namespace 'StackFrameInternal
備註
執行緒執行過程中,每個函式呼叫都會建立並推送 A StackFrame 到呼叫堆疊上。 堆疊框架始終包含 MethodBase 資訊,並可選擇性地包含檔案名稱、行號及欄號資訊。
StackFrame 除錯建置設定的資訊最為有用。 預設情況下,除錯建置包含除錯符號,而 Release 建置則沒有。 除錯符號包含大部分用於建構 StackFrame 物件的檔案、方法名稱、行號與欄位資訊。
建構函式
| 名稱 | Description |
|---|---|
| StackFrame() |
初始化 StackFrame 類別的新執行個體。 |
| StackFrame(Boolean) |
初始化該類別的新實例 StackFrame ,並可選擇擷取來源資訊。 |
| StackFrame(Int32, Boolean) |
初始化一個對應於目前堆疊框架上方一幀的新類別實例 StackFrame ,並可選擇性地擷取原始資訊。 |
| StackFrame(Int32) |
初始化一個對應於目前堆疊框架上方一個框架的新類別實例 StackFrame 。 |
| StackFrame(String, Int32, Int32) |
初始化一個僅包含指定檔名、行號和欄號的類別新實例 StackFrame 。 |
| StackFrame(String, Int32) |
初始化一個只包含指定檔案名稱與行號的新類別實例 StackFrame 。 |
欄位
| 名稱 | Description |
|---|---|
| OFFSET_UNKNOWN |
定義當本地或Microsoft中介語言(MSIL)偏移未知時,GetNativeOffset() 或 GetILOffset() 方法回傳的值。 此欄位是常數。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的 物件是否等於目前的物件。 (繼承來源 Object) |
| GetFileColumnNumber() |
取得包含正在執行程式碼的檔案中的欄位編號。 這些資訊通常是從可執行檔的除錯符號中擷取的。 |
| GetFileLineNumber() |
取得包含正在執行程式碼的檔案行號。 這些資訊通常是從可執行檔的除錯符號中擷取的。 |
| GetFileName() |
取得包含正在執行程式碼的檔名。 這些資訊通常是從可執行檔的除錯符號中擷取的。 |
| GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
| GetILOffset() |
取得執行方法的 Microsoft 中介語言(MSIL)程式碼開始的偏移量。 這個偏移量可能是近似值,取決於即時編譯器是否產生除錯程式碼。 這些除錯資訊的產生由 DebuggableAttribute控制。 |
| GetMethod() |
取得該框架執行的方法。 |
| GetNativeOffset() |
取得該方法的原生即時編譯程式碼起點的偏移量。 這些除錯資訊的產生由 DebuggableAttribute 類別控制。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
建立可讀的堆疊追蹤表示。 |
擴充方法
| 名稱 | Description |
|---|---|
| GetNativeImageBase(StackFrame) |
回傳指向該堆疊框架正在執行的原生映像的基址指標。 |
| GetNativeIP(StackFrame) |
會取得一個介面指標,指向正在執行的方法的原生程式碼開頭。 |
| HasILOffset(StackFrame) |
表示執行中方法是否可用從 IL 程式碼起始點的偏移量。 |
| HasMethod(StackFrame) |
表示是否提供指定幀執行方法的資訊。 |
| HasNativeImage(StackFrame) |
表示該堆疊框架是否可用原生映像檔。 |
| HasSource(StackFrame) |
表示包含指定堆疊框架正在執行的程式碼的檔案是否可用。 |