Click here to Skip to main content
15,888,527 members
Home / Discussions / C#
   

C#

 
GeneralRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre30-Jan-16 8:06
professionalSascha Lefèvre30-Jan-16 8:06 
AnswerRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre30-Jan-16 11:42
professionalSascha Lefèvre30-Jan-16 11:42 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 1:19
mvaKenneth Haugland31-Jan-16 1:19 
GeneralRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre31-Jan-16 1:42
professionalSascha Lefèvre31-Jan-16 1:42 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 4:24
mvaKenneth Haugland31-Jan-16 4:24 
GeneralRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre31-Jan-16 6:34
professionalSascha Lefèvre31-Jan-16 6:34 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 7:29
mvaKenneth Haugland31-Jan-16 7:29 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 11:27
mvaKenneth Haugland31-Jan-16 11:27 
Your code works very well, but I don't really like changing the pattern from the original implementation, so I came up with this:
C#
    internal class WeakPropertyChangedSubscriber
    {
        public WeakPropertyChangedSubscriber(object WeakClass, IObservable<EventPattern<PropertyChangedEventArgs>> observable,ref IDisposable Result, Action<EventPattern<PropertyChangedEventArgs>> eventAction)
        {
            Result = observable.InternalSubscribeToWeakPropertyChange(eventAction, WeakClass, WeakPropertyChangedSubscriber.HandleEvent);
        }

        public static void HandleEvent(Action<EventPattern<PropertyChangedEventArgs>> subscriber, object reffing, EventPattern<PropertyChangedEventArgs> item)
        {
        subscriber(item);
        }
    }

    public static IDisposable SubscribeWeakly<T>(this IObservable<T> observable, object WeakClass, Action<EventPattern<PropertyChangedEventArgs>> onNext)
    {
        IDisposable Result = null;
        IObservable<EventPattern<PropertyChangedEventArgs>> ObservableCast = (IObservable<EventPattern<PropertyChangedEventArgs>>)observable;
        WeakPropertyChangedSubscriber WeakReferanceChanged = new WeakPropertyChangedSubscriber(WeakClass, ObservableCast, ref Result, onNext);
        return Result;
    }

    private static IDisposable InternalSubscribeToWeakPropertyChange<TEventPattern, TSubscriber, TWeakReferance>(this IObservable<TEventPattern> observable, TSubscriber subscriber, TWeakReferance refing, Action<TSubscriber, TWeakReferance, TEventPattern> onNext)
where TSubscriber : class where TWeakReferance : class
    {
        if (onNext.Target != null)
            throw new ArgumentException("onNext must refer to a static method, or else the subscription will still hold a strong reference to target");

        var reference = new WeakReference(refing);

        IDisposable subscription = null;
        subscription = observable.Subscribe(item =>
        {
            var currentTarget = reference.Target as TWeakReferance;
            if (currentTarget != null)
            {
                onNext(subscriber,currentTarget, item);
            }
            else
            {
                Console.WriteLine("subscription.Dispose()");
                subscription.Dispose();
            }
        });
        return subscription;
    }


This way you can use it almost like the normal subscribe. Only difference is that you have to say what object that should have a weak reference:
C#
var er = ObservableEx.Observe(C, o => o.Name).SubscribeWeakly(C,arg => PropertyChanged(arg.Sender, arg.EventArgs));
C.Name = "test";
C = null;
GC.Collect();
C = new ViewModelB();
C.Name = "2";

There has to be a way to get the class trough reflection? I coudnt find it now though.
QuestionTableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
Emanuele Bonin30-Jan-16 1:24
Emanuele Bonin30-Jan-16 1:24 
Answer[SOLVED] TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
Emanuele Bonin30-Jan-16 2:17
Emanuele Bonin30-Jan-16 2:17 
GeneralRe: [SOLVED] TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
BillWoodruff30-Jan-16 2:46
professionalBillWoodruff30-Jan-16 2:46 
GeneralRe: [SOLVED] TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
Emanuele Bonin30-Jan-16 6:18
Emanuele Bonin30-Jan-16 6:18 
AnswerRe: TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
BillWoodruff30-Jan-16 2:39
professionalBillWoodruff30-Jan-16 2:39 
QuestionSubscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 2:18
mvaKenneth Haugland29-Jan-16 2:18 
AnswerRe: Subscribe to property changes using Reactive Extensions Pin
Pete O'Hanlon29-Jan-16 3:25
mvePete O'Hanlon29-Jan-16 3:25 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 4:47
mvaKenneth Haugland29-Jan-16 4:47 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Richard Deeming29-Jan-16 5:03
mveRichard Deeming29-Jan-16 5:03 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 5:22
mvaKenneth Haugland29-Jan-16 5:22 
AnswerRe: Subscribe to property changes using Reactive Extensions Pin
Richard Deeming29-Jan-16 3:31
mveRichard Deeming29-Jan-16 3:31 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 4:56
mvaKenneth Haugland29-Jan-16 4:56 
AnswerRe: Subscribe to property changes using Reactive Extensions Pin
Sascha Lefèvre29-Jan-16 3:49
professionalSascha Lefèvre29-Jan-16 3:49 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 4:59
mvaKenneth Haugland29-Jan-16 4:59 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Sascha Lefèvre29-Jan-16 5:30
professionalSascha Lefèvre29-Jan-16 5:30 
GeneralRe: Subscribe to property changes using Reactive Extensions Pin
Kenneth Haugland29-Jan-16 5:51
mvaKenneth Haugland29-Jan-16 5:51 
QuestionEnable / Disable DNS Client service (c#) Pin
Member 1041097228-Jan-16 22:21
Member 1041097228-Jan-16 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.