C# Wishlist: Post-Constructor Method

Software

I would love it if C# had a predefined method that would be guaranteed to be called after every constructor is done firing. I have some checking code that I need to call, but depending on which constructors are called, it could need to be fired in a few different places. If I had a PostConstructor() method or something, I could centralize the checking code there.

It would also be cool if you could provide a method that would be called before or after every method call, like the SetUp() and TearDown() special methods in NUnit, but without all the reflection overhead at runtime.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Furl
  • Ma.gnolia
  • Reddit
  • TwitThis
3 Comments

3 Comments

  1. W. Kevin Hazzard  •  Jan 31, 2008 @12:00 pm

    You could, of course, place the construction in a try block and in the finally block, do your post construction proessing. The finally block will alsways run. — Kevin

  2. Anthony Stevens  •  Jan 31, 2008 @12:04 pm

    Hm … wouldn’t I have to put a try/finally block in every constructor? That’s what I would like to avoid. Or are you thinking that the callers would new() in a try/finally block?

  3. Raja Nagendra Kumar  •  Oct 1, 2008 @3:23 am

    We too are looking for such feature, in another context..

    It is in the inheritance chain, few methods needs to be call after both parent and child constructors are called.. such as display of the UI screen..

    Showing it in parent would mean, before child adds all its specialisations, the form would get displayed etc..

    I would see this to be OOAD extention feature..

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">