mscorlib(4.0.0.0) API with additions
StackFrame.cs
1 using System.Reflection;
4 using System.Security;
6 using System.Text;
7 
8 namespace System.Diagnostics
9 {
11  [Serializable]
12  [ComVisible(true)]
13  [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
14  public class StackFrame
15  {
16  private MethodBase method;
17 
18  private int offset;
19 
20  private int ILOffset;
21 
22  private string strFileName;
23 
24  private int iLineNumber;
25 
26  private int iColumnNumber;
27 
28  [OptionalField]
29  private bool fIsLastFrameFromForeignExceptionStackTrace;
30 
32  public const int OFFSET_UNKNOWN = -1;
33 
34  internal void InitMembers()
35  {
36  method = null;
37  offset = -1;
38  ILOffset = -1;
39  strFileName = null;
40  iLineNumber = 0;
41  iColumnNumber = 0;
42  fIsLastFrameFromForeignExceptionStackTrace = false;
43  }
44 
46  public StackFrame()
47  {
48  InitMembers();
49  BuildStackFrame(0, fNeedFileInfo: false);
50  }
51 
55  public StackFrame(bool fNeedFileInfo)
56  {
57  InitMembers();
58  BuildStackFrame(0, fNeedFileInfo);
59  }
60 
63  public StackFrame(int skipFrames)
64  {
65  InitMembers();
66  BuildStackFrame(skipFrames + 0, fNeedFileInfo: false);
67  }
68 
73  public StackFrame(int skipFrames, bool fNeedFileInfo)
74  {
75  InitMembers();
76  BuildStackFrame(skipFrames + 0, fNeedFileInfo);
77  }
78 
79  internal StackFrame(bool DummyFlag1, bool DummyFlag2)
80  {
81  InitMembers();
82  }
83 
87  public StackFrame(string fileName, int lineNumber)
88  {
89  InitMembers();
90  BuildStackFrame(0, fNeedFileInfo: false);
91  strFileName = fileName;
92  iLineNumber = lineNumber;
93  iColumnNumber = 0;
94  }
95 
100  public StackFrame(string fileName, int lineNumber, int colNumber)
101  {
102  InitMembers();
103  BuildStackFrame(0, fNeedFileInfo: false);
104  strFileName = fileName;
105  iLineNumber = lineNumber;
106  iColumnNumber = colNumber;
107  }
108 
109  internal virtual void SetMethodBase(MethodBase mb)
110  {
111  method = mb;
112  }
113 
114  internal virtual void SetOffset(int iOffset)
115  {
116  offset = iOffset;
117  }
118 
119  internal virtual void SetILOffset(int iOffset)
120  {
121  ILOffset = iOffset;
122  }
123 
124  internal virtual void SetFileName(string strFName)
125  {
126  strFileName = strFName;
127  }
128 
129  internal virtual void SetLineNumber(int iLine)
130  {
131  iLineNumber = iLine;
132  }
133 
134  internal virtual void SetColumnNumber(int iCol)
135  {
136  iColumnNumber = iCol;
137  }
138 
139  internal virtual void SetIsLastFrameFromForeignExceptionStackTrace(bool fIsLastFrame)
140  {
141  fIsLastFrameFromForeignExceptionStackTrace = fIsLastFrame;
142  }
143 
144  internal virtual bool GetIsLastFrameFromForeignExceptionStackTrace()
145  {
146  return fIsLastFrameFromForeignExceptionStackTrace;
147  }
148 
151  public virtual MethodBase GetMethod()
152  {
153  return method;
154  }
155 
158  public virtual int GetNativeOffset()
159  {
160  return offset;
161  }
162 
165  public virtual int GetILOffset()
166  {
167  return ILOffset;
168  }
169 
172  [SecuritySafeCritical]
173  public virtual string GetFileName()
174  {
175  if (strFileName != null)
176  {
177  FileIOPermission fileIOPermission = new FileIOPermission(PermissionState.None);
178  fileIOPermission.AllFiles = FileIOPermissionAccess.PathDiscovery;
179  fileIOPermission.Demand();
180  }
181  return strFileName;
182  }
183 
186  public virtual int GetFileLineNumber()
187  {
188  return iLineNumber;
189  }
190 
193  public virtual int GetFileColumnNumber()
194  {
195  return iColumnNumber;
196  }
197 
200  [SecuritySafeCritical]
201  public override string ToString()
202  {
203  StringBuilder stringBuilder = new StringBuilder(255);
204  if (method != null)
205  {
206  stringBuilder.Append(method.Name);
207  if (method is MethodInfo && ((MethodInfo)method).IsGenericMethod)
208  {
209  Type[] genericArguments = ((MethodInfo)method).GetGenericArguments();
210  stringBuilder.Append("<");
211  int i = 0;
212  bool flag = true;
213  for (; i < genericArguments.Length; i++)
214  {
215  if (!flag)
216  {
217  stringBuilder.Append(",");
218  }
219  else
220  {
221  flag = false;
222  }
223  stringBuilder.Append(genericArguments[i].Name);
224  }
225  stringBuilder.Append(">");
226  }
227  stringBuilder.Append(" at offset ");
228  if (offset == -1)
229  {
230  stringBuilder.Append("<offset unknown>");
231  }
232  else
233  {
234  stringBuilder.Append(offset);
235  }
236  stringBuilder.Append(" in file:line:column ");
237  bool flag2 = strFileName != null;
238  if (flag2)
239  {
240  try
241  {
242  FileIOPermission fileIOPermission = new FileIOPermission(PermissionState.None);
243  fileIOPermission.AllFiles = FileIOPermissionAccess.PathDiscovery;
244  fileIOPermission.Demand();
245  }
246  catch (SecurityException)
247  {
248  flag2 = false;
249  }
250  }
251  if (!flag2)
252  {
253  stringBuilder.Append("<filename unknown>");
254  }
255  else
256  {
257  stringBuilder.Append(strFileName);
258  }
259  stringBuilder.Append(":");
260  stringBuilder.Append(iLineNumber);
261  stringBuilder.Append(":");
262  stringBuilder.Append(iColumnNumber);
263  }
264  else
265  {
266  stringBuilder.Append("<null>");
267  }
268  stringBuilder.Append(Environment.NewLine);
269  return stringBuilder.ToString();
270  }
271 
272  private void BuildStackFrame(int skipFrames, bool fNeedFileInfo)
273  {
274  using (StackFrameHelper stackFrameHelper = new StackFrameHelper(null))
275  {
276  stackFrameHelper.InitializeSourceInfo(0, fNeedFileInfo, null);
277  int numberOfFrames = stackFrameHelper.GetNumberOfFrames();
278  skipFrames += StackTrace.CalculateFramesToSkip(stackFrameHelper, numberOfFrames);
279  if (numberOfFrames - skipFrames > 0)
280  {
281  method = stackFrameHelper.GetMethodBase(skipFrames);
282  offset = stackFrameHelper.GetOffset(skipFrames);
283  ILOffset = stackFrameHelper.GetILOffset(skipFrames);
284  if (fNeedFileInfo)
285  {
286  strFileName = stackFrameHelper.GetFilename(skipFrames);
287  iLineNumber = stackFrameHelper.GetLineNumber(skipFrames);
288  iColumnNumber = stackFrameHelper.GetColumnNumber(skipFrames);
289  }
290  }
291  }
292  }
293  }
294 }
static string NewLine
Gets the newline string defined for this environment.
Definition: Environment.cs:449
Describes a set of security permissions applied to code. This class cannot be inherited.
FileIOPermissionAccess
Specifies the type of file access requested.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
StackFrame(string fileName, int lineNumber)
Initializes a new instance of the T:System.Diagnostics.StackFrame class that contains only the given ...
Definition: StackFrame.cs:87
Represents a stack trace, which is an ordered collection of one or more stack frames.
Definition: StackTrace.cs:16
StackFrame(string fileName, int lineNumber, int colNumber)
Initializes a new instance of the T:System.Diagnostics.StackFrame class that contains only the given ...
Definition: StackFrame.cs:100
Definition: __Canon.cs:3
virtual int GetFileColumnNumber()
Gets the column number in the file that contains the code that is executing. This information is typi...
Definition: StackFrame.cs:193
virtual Type [] GetGenericArguments()
Returns an array of T:System.Type objects that represent the type arguments of a closed generic type ...
Definition: Type.cs:2433
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
virtual int GetILOffset()
Gets the offset from the start of the Microsoft intermediate language (MSIL) code for the method that...
Definition: StackFrame.cs:165
StackFrame(int skipFrames)
Initializes a new instance of the T:System.Diagnostics.StackFrame class that corresponds to a frame a...
Definition: StackFrame.cs:63
override string ToString()
Builds a readable representation of the stack trace.
Definition: StackFrame.cs:201
StackFrame(bool fNeedFileInfo)
Initializes a new instance of the T:System.Diagnostics.StackFrame class, optionally capturing source ...
Definition: StackFrame.cs:55
virtual int GetNativeOffset()
Gets the offset from the start of the native just-in-time (JIT)-compiled code for the method that is ...
Definition: StackFrame.cs:158
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
virtual MethodBase GetMethod()
Gets the method in which the frame is executing.
Definition: StackFrame.cs:151
Provides information about methods and constructors.
Definition: MethodBase.cs:19
StackFrame(int skipFrames, bool fNeedFileInfo)
Initializes a new instance of the T:System.Diagnostics.StackFrame class that corresponds to a frame a...
Definition: StackFrame.cs:73
virtual int GetFileLineNumber()
Gets the line number in the file that contains the code that is executing. This information is typica...
Definition: StackFrame.cs:186
FileIOPermissionAccess AllFiles
Gets or sets the permitted access to all files.
abstract string Name
Gets the name of the current member.
Definition: MemberInfo.cs:27
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
Specifies that the class can be serialized.
const int OFFSET_UNKNOWN
Defines the value that is returned from the M:System.Diagnostics.StackFrame.GetNativeOffset or M:Syst...
Definition: StackFrame.cs:32
Provides information about a T:System.Diagnostics.StackFrame, which represents a function call on the...
Definition: StackFrame.cs:14
StackFrame()
Initializes a new instance of the T:System.Diagnostics.StackFrame class.
Definition: StackFrame.cs:46
Controls the ability to access files and folders. This class cannot be inherited.
The exception that is thrown when a security error is detected.
virtual string GetFileName()
Gets the file name that contains the code that is executing. This information is typically extracted ...
Definition: StackFrame.cs:173