Attach to Property's setter

Posted by StupidDeveloper on Stack Overflow See other posts from Stack Overflow or by StupidDeveloper
Published on 2010-03-14T18:46:44Z Indexed on 2010/03/14 18:55 UTC
Read the original article Hit count: 191

Filed under:

I haven't found similiar post so I'm asking this.
Let's say I defined somewhere an application wide available static Property (I mean it's not local) and in one class I would like to know when this property is being changed. Apart from aop (transparentproxy etc.) which I think doesn't suit me well here (and I can't add that to project anyway), what are the options here?

One solution I can think of, which is probably a very nasty one, is to use some event that would be executed in the setter and just attach it in the class(es) which needs that. Something like:

   public static event EventHandler CurrentNumberChanged= delegate {};
   public static int CurrentNumber
    {
        get
        {
            return currentNumber;
        }
        set
        {
            currentNumber = value;

            CurrentNumberChanged(null, EventArgs.Empty);
        }
    }

I know it's really unsafe to use such events ( read here ). And since I would use it in asp.net makes it even more ugly. Do you have any advices ?

© Stack Overflow or respective owner

Related posts about c#