Invoking Methods


In this chapter we will illustrate the different kinds of methods, parameter types, and other considerations when designing classes in C#. First, let's consider what happens inside the .NET Common Language Runtime when a method is actually invoked. Method invocation consists of a number of pre-processing and post-processing activities. Initially, the CLR allocates memory to the method, by creating a stack frame. A stack frame is a block of stack-based memory that is large enough to accommodate the arguments that are passed to the method as well as local variables defined within the method itself. A stack frame is a temporary block of memory because when method execution ends, the frame is popped off the stack immediately freeing up memory.

All applications from simple console ones through to large multi-tier distributed systems are ultimately built up through sequences of method calls. When sequences of methods are invoked, a stack frame is created for each method and added to the call stack. This process builds up a stack trace. Since a stack trace is an ordered collection of stack frames, it actually provides a detailed log of the sequences of method calls made within an application. Each stack frame contains the arguments passed into the method and all variables within it.

Stack frame and stack trace information is most useful in debugging situations where you need to know the sequence of calls from one area of the application to the next. You also need to know what arguments were passed to each method and what happens to those arguments inside the method.

The .NET Framework permits access to the stack frame and stack trace data through the StackFrame and StackTrace classes in the System.Diagnostics namespace. The following example shows how to use these classes, and the source code can be found in stack_trace.cs:

    using System;    using System.Diagnostics;    class Foo    {      static void Main()      {        Bar.BarMethod(42);      }    }    class Bar    {      public static void BarMethod(int x)      {        StackTrace st = new StackTrace();        for(int i=0; i < st.FrameCount; i++)        {          StackFrame sf = new StackFrame();          sf = st.GetFrame(i);          Console.WriteLine(sf.GetMethod());        }      }    } 

This example invokes a static method called Bar.BarMethod(). This method dumps a simple stack trace to the console window by iterating through the internal collection of stack frame objects returned from the StackTrace.GetFrame() method. The following output should be displayed in the Console window:

    C:\Class Design\Ch 03>stack_trace    Void BarMethod(Int32)    Void Main() 

We can see both the methods making up the stack trace, with the most recent first. The information displayed here is quite similar to some of the debug messages produced in Visual Studio .NET when creating your own applications. The System.Diagnostics namespace classes provide a good way to help debug the methods in your application. Using guidelines from the above example, you can create your own custom stack traces and discover which methods called your methods.




C# Class Design Handbook(c) Coding Effective Classes
C# Class Design Handbook: Coding Effective Classes
ISBN: 1590592573
EAN: 2147483647
Year: N/A
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net