Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Weak Event seems to fail in test method

Weak Event seems to fail in test method

Scheduled Pinned Locked Moved C#
linqtestingfunctionalhelp
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #1

    EDIT this was an accidental post, problem is fixed!! I am running / fixing all my unit test for my home made utilities. I have a weak event build method which look like that

        public static PropertyChangedEventHandler AddWeakHandler<T>(this INotifyPropertyChanged model, T target, Action<T, PropertyChangedEventArgs> action)
            where T : class
        {
            var weakRef = new WeakReference(target);
            PropertyChangedEventHandler handler = null;
            handler = new PropertyChangedEventHandler(
                (s, e) =>
                {
                    var strongRef = weakRef.Target as T;
                    if (strongRef != null)
                    {
                        action(strongRef, e);
                    }
                    else
                    {
                        model.PropertyChanged -= handler;
                        handler = null;
                    }
                });
            model.PropertyChanged += handler;
            return handler;
        }
    

    it is used like that (to show that there are no captured variable in the lambda expression

                public object Source
                {
                    get { return mSource; }
                    set
                    {
                        var p = Property;
                        if (p == null)
                            value = null;
                        if (value == mSource)
                            return;
    
                        if (Source is INotifyPropertyChanged)
                        {
                            ((INotifyPropertyChanged)Source).PropertyChanged -= previous;
                            previous = null;
                        }
                        mSource = value;
                        if (Source is INotifyPropertyChanged)
                        {
                            previous = **WeakEvents.AddWeakHandler**((INotifyPropertyChanged)Source, this, (x, arg) => x.OnSourcePropertyChanged(Source, arg));
                        }
                        if (p != null)
                        {
                            p.OnPropertyChanged();
                        }
                    }
                }
    

    I have a simple (XUnit) test looking like that

    	\[Fact\]
        public void CheckPropertyPathIsWeakEvent()
    	{
    		var m = new ModelForPathCheck();
    		int n = 0;
    		Action<string
    
    K 1 Reply Last reply
    0
    • S Super Lloyd

      EDIT this was an accidental post, problem is fixed!! I am running / fixing all my unit test for my home made utilities. I have a weak event build method which look like that

          public static PropertyChangedEventHandler AddWeakHandler<T>(this INotifyPropertyChanged model, T target, Action<T, PropertyChangedEventArgs> action)
              where T : class
          {
              var weakRef = new WeakReference(target);
              PropertyChangedEventHandler handler = null;
              handler = new PropertyChangedEventHandler(
                  (s, e) =>
                  {
                      var strongRef = weakRef.Target as T;
                      if (strongRef != null)
                      {
                          action(strongRef, e);
                      }
                      else
                      {
                          model.PropertyChanged -= handler;
                          handler = null;
                      }
                  });
              model.PropertyChanged += handler;
              return handler;
          }
      

      it is used like that (to show that there are no captured variable in the lambda expression

                  public object Source
                  {
                      get { return mSource; }
                      set
                      {
                          var p = Property;
                          if (p == null)
                              value = null;
                          if (value == mSource)
                              return;
      
                          if (Source is INotifyPropertyChanged)
                          {
                              ((INotifyPropertyChanged)Source).PropertyChanged -= previous;
                              previous = null;
                          }
                          mSource = value;
                          if (Source is INotifyPropertyChanged)
                          {
                              previous = **WeakEvents.AddWeakHandler**((INotifyPropertyChanged)Source, this, (x, arg) => x.OnSourcePropertyChanged(Source, arg));
                          }
                          if (p != null)
                          {
                              p.OnPropertyChanged();
                          }
                      }
                  }
      

      I have a simple (XUnit) test looking like that

      	\[Fact\]
          public void CheckPropertyPathIsWeakEvent()
      	{
      		var m = new ModelForPathCheck();
      		int n = 0;
      		Action<string
      
      K Offline
      K Offline
      Kenneth Haugland
      wrote on last edited by
      #2

      I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.

      S 4 Replies Last reply
      0
      • K Kenneth Haugland

        I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #3

        My bad... I should not have posted that!!! Garbage collection worked fine (as test finalizer indicated) but the event was spuriously triggered in the PropertyPath constructor! all solved! ^^

        All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

        1 Reply Last reply
        0
        • K Kenneth Haugland

          I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #4

          on a side note I didn't understand your confusion! ;P

          All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

          1 Reply Last reply
          0
          • K Kenneth Haugland

            I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #5

            full source code, for your understanding!

            public class PropertyPath<TProp> : PropertyPath
            {
            internal PropertyPath()
            {
            }
            public new TProp Value
            {
            get
            {
            var v = base.Value;
            if (v is TProp)
            return (TProp)v;
            return default(TProp);
            }
            set { base.Value = value; }
            }
            }
            public class PropertyPath : ModelBase
            {
            public static PropertyPath<TP> Link<T, TP>(T root, Expression<Func<T, TP>> e, Action<TP> onValueChanged)
            {
            var pv = new PropertyPath<TP>();
            pv.PropertyChanged += (o, eargs) =>
            {
            if (string.IsNullOrEmpty(eargs.PropertyName) || eargs.PropertyName == "Value")
            {
            onValueChanged(pv.Value);
            }
            };
            pv.Initialize(e);
            pv.Root = root;
            return pv;
            }
            public static PropertyPath<TP> Link<TP>(Expression<Func<TP>> e, Action<TP> onValueChanged)
            {
            var pv = new PropertyPath<TP>();
            pv.PropertyChanged += (o, eargs) =>
            {
            if (string.IsNullOrEmpty(eargs.PropertyName) || eargs.PropertyName == "Value")
            {
            onValueChanged(pv.Value);
            }
            };
            pv.Initialize(e);
            return pv;
            }

                public static PropertyPath<TP> Create<T, TP>(T root, Expression<Func<T, TP>> e)
                {
                    var pv = new PropertyPath<TP>();
                    pv.Initialize(e);
                    pv.Root = root;
                    return pv;
                }
                public static PropertyPath<TP> Create<TP>(Expression<Func<TP>> e)
                {
                    var pv = new PropertyPath<TP>();
                    pv.Initialize(e);
                    return pv;
                }
            
                internal PropertyPath()
                {
                }
                void Initialize(LambdaExpression e)
                {
                    object root;
                    var path = ReflectionEx.GetLambdaPath(e, out root);
                    if (path.Length == 0)
                        throw new ArgumentException();
                    pvalues = new PropertyValue\[path.Length\];
                    roValues = new ReadOnlyCollection<PropertyValue>(pvalues);
                    for (int i = 0
            
            1 Reply Last reply
            0
            • K Kenneth Haugland

              I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.

              S Offline
              S Offline
              Super Lloyd
              wrote on last edited by
              #6

              What you might have misread... the class is NOT registering a listener to its own change! It's listening to change in the source object (!= this!) !!

              All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

              K 1 Reply Last reply
              0
              • S Super Lloyd

                What you might have misread... the class is NOT registering a listener to its own change! It's listening to change in the source object (!= this!) !!

                All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                K Offline
                K Offline
                Kenneth Haugland
                wrote on last edited by
                #7

                No worries, I just saw the little code snippet and I don't know what your requirements are. My knee jerk reaction was that there has to be a simpler way of doing this. Btw, there is a standard WeakEventManager inside .NET (first intoduced in 3.0) WeakEventManager Class (System.Windows)[^]

                S 1 Reply Last reply
                0
                • K Kenneth Haugland

                  No worries, I just saw the little code snippet and I don't know what your requirements are. My knee jerk reaction was that there has to be a simpler way of doing this. Btw, there is a standard WeakEventManager inside .NET (first intoduced in 3.0) WeakEventManager Class (System.Windows)[^]

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #8

                  It's not supported by the PCL!!! But no worries, it's simple enough to implement that I made my own PCL version! :-D

                  All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                  K 1 Reply Last reply
                  0
                  • S Super Lloyd

                    It's not supported by the PCL!!! But no worries, it's simple enough to implement that I made my own PCL version! :-D

                    All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                    K Offline
                    K Offline
                    Kenneth Haugland
                    wrote on last edited by
                    #9

                    PCL?

                    S 1 Reply Last reply
                    0
                    • K Kenneth Haugland

                      PCL?

                      S Offline
                      S Offline
                      Super Lloyd
                      wrote on last edited by
                      #10

                      Cross-Platform Development with the Portable Class Library[^] I.e. PCL class library support normal .NET (4.5), UWP, Xamarin iOS, Xamarin Android, and the upcoming .NET Core / .NET native!

                      All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups