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 ?
You could use a variation on the Observer pattern to the same effect. Not sure what your threading requirements are, and I suspect this suffers from similar dereferencing problems as How to raise custom event from a Static Class (although would have to play with the code a bit more to bottom that out):
using System;
using System.Collections.Generic;
namespace ClassLibrary1
{
public class StaticObservable
{
private static int currentNumber;
}