Information Technology Reference
In-Depth Information
private void CheckState()
{
// Grab the name of the calling routine:
string methodName =
new StackTrace ().GetFrame( 1 ).GetMethod().Name;
Trace .WriteLine( "Entering CheckState for Person:" );
Trace .Write( "\tcalled by " );
Trace .WriteLine(methodName);
Debug .Assert(lastName != null ,
methodName,
"Last Name cannot be null" );
Debug .Assert(lastName.Length > 0 ,
methodName,
"Last Name cannot be blank" );
Debug .Assert(firstName != null ,
methodName,
"First Name cannot be null" );
Debug .Assert(firstName.Length > 0 ,
methodName,
"First Name cannot be blank" );
Trace .WriteLine( "Exiting CheckState for Person" );
}
Yo u m i g h t n o t h a v e e n c o u n t e r e d m a n y l i b r a r y f u n c t i o n s i n t h i s m e t h o d ,
so let's go over them briefly. The StackTrace class gets the name of the call-
ing method using Reflection. It's rather expensive, but it greatly simplifies
tasks, such as generating information about program flow. Here, it deter-
mines the name of the method called CheckState. There is a minor risk
here if the calling method is inlined, but the alternative is to have each
method that calls CheckState() pass in the method name using Method-
Base.GetCurrentMethod(). You'll see shortly why I decided against that
strategy.
The remaining methods are part of the System.Diagnostics.Debug class
or the System.Diagnostics.Trace class. The Debug.Assert method tests a
 
Search WWH ::




Custom Search