开发者

Attach to Property's setter

开发者 https://www.devze.com 2022-12-23 09:22 出处:网络
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 thi

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
        {
  开发者_如何学Go          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;

    private static readonly List<IObserver> observers = new List<IObserver>();

    public static int CurrentNumber
    {
        get{return currentNumber;}
        set
        {
            currentNumber = value;
            foreach (var observer in observers)
            {
                observer.NotifyChange();
            }
        }
    }

    public static void Attach(IObserver observer)
    {
        observers.Add(observer);
    }

    public static void Detach(IObserver observer)
    {
        observers.Remove(observer);
    }
}

public interface IObserver
{
    void NotifyChange();
}

public class ObserverImpl : IObserver
{
    public void NotifyChange()
    {
        Console.Out.WriteLine("Number has changed");
    }
}


public class AppWrapper
{
    public static void Main (string[] args)
    {
        Console.ReadLine();

        var observerImpl1 = new ObserverImpl();
        var observerImpl2 = new ObserverImpl();

        StaticObservable.Attach(observerImpl1);
        StaticObservable.Attach(observerImpl2);

        StaticObservable.CurrentNumber = 1;

        Console.ReadLine();
    }
}

}


The answer you mentioned just said that if you forget to unsubscribe some instace from a static event then this instance will live forever.


EventHandler is a bit too much I think, why not just create some boolean flag with a setter in the class that needs to receive the message, and whenever CurrentNumber's setter is triggered, call the setter of that boolean flag.
I'd like to be a little more descriptive here, but the data is not sufficient to suggest actual code.

0

精彩评论

暂无评论...
验证码 换一张
取 消