Does .NET Support Custom Blocks?

  • Thread starter Thread starter Alphamacaroon
  • Start date Start date
A

Alphamacaroon

All,

Does .NET support the concept of "custom" blocks?

I'd like to implement some performance profiling code into an app I'm
creating and I thought it would be really cool if you could do
something like:

profile("ProfileName") // Starts a timer
{
MyFunction1();
MyFunction2();
} // Ends a timer and writes it to a log

The concept would be to allow me to surround a group of code with a
custom block and be able to stop and start a timer based upon when it
entered and exited the block.

I know this could easily be accomplished by just adding a function
before and after, but the block would be much more elegant. Also it
would allow you to nicely accomplish the timer if for example the
second line of code (MyFunction2()) returned a value.

Can this be done?

-alpha
 
Alphamacaroon said:
Does .NET support the concept of "custom" blocks?

Well, you're not really after .NET support here so much as language
support.
I'd like to implement some performance profiling code into an app I'm
creating and I thought it would be really cool if you could do
something like:

profile("ProfileName") // Starts a timer
{
MyFunction1();
MyFunction2();
} // Ends a timer and writes it to a log

The concept would be to allow me to surround a group of code with a
custom block and be able to stop and start a timer based upon when it
entered and exited the block.

I know this could easily be accomplished by just adding a function
before and after, but the block would be much more elegant. Also it
would allow you to nicely accomplish the timer if for example the
second line of code (MyFunction2()) returned a value.

Can this be done?

A using statement would probably be the easiest way of accomplishing
this - just make the returned value implement IDisposable. See
http://pobox.com/~skeet/csharp/miscutil/usage/locking.html for an
example in terms of locks.
 
Back
Top