user5545683
user5545683

Reputation:

castle dynamic proxy PropertyChanged called before value changed

I create an unit test library with the following code, it tries to proxy an INotifyPropertyChanged, and listen on it.

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Castle.Core;
using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Component = Castle.MicroKernel.Registration.Component;

namespace UnitTestWinsorContainer {
    [TestClass]
    public class UnitTestDynamicProxy {
        public static IWindsorContainer Container { get; protected set; }

        [ClassInitialize]
        public static void TestInit(TestContext tc) {
            Container = new WindsorContainer();
            Container.Register(Classes.FromThisAssembly().BasedOn<IInterceptor>());
            Container.Register(Component.For(typeof(DummyViewModel))
                .Interceptors(InterceptorReference.ForType<DummyInterceptor>()).Anywhere);
        }

        [TestMethod]
        public void TestResolve() {
            Console.WriteLine("Test Starts");
            var i = Container.Resolve<DummyViewModel>();
            Assert.AreEqual(i.GetType().Name, "DummyViewModelProxy");
            i.PropertyChanged += OnPropertyChanged;
            i.Qty = 100;
            Console.WriteLine($"Get Qty after PropertyChanged of Proxy Qty=[{i.Qty}]");

            var j = new DummyViewModel();
            j.PropertyChanged += OnPropertyChanged;
            j.Qty = 100;
        }

        private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e) {
            var d = sender as DummyViewModel;
            if (d != null) Console.WriteLine($"PropertyName=[{e.PropertyName}] Qty=[{d.Qty}]");
        }
    }

    public class DummyInterceptor : IInterceptor {
        public void Intercept(IInvocation invocation) {
            invocation.Proceed();
        }
    }

    public class DummyViewModel : Bindable {
        private int qty;

        /// <summary>
        ///     My Property
        /// </summary>
        public int Qty {
            get { return qty; }
            set { SetProperty(ref qty, value); }
        }
    }

    public class Bindable : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) {
            if (Equals(storage, value)) return false;

            storage = value;
            RaisePropertyChanged(propertyName);

            return true;
        }

        protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged,
            [CallerMemberName] string propertyName = null) {
            if (Equals(storage, value)) return false;

            storage = value;
            onChanged?.Invoke();
            RaisePropertyChanged(propertyName);

            return true;
        }

        protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) {
            PropertyChanged?.Invoke(this, args);
        }
    }
}

test will succeed, but the output console writes

Test Starts
PropertyName=[Qty] Qty=[0]
Get Qty after PropertyChanged of Proxy Qty=[100]
PropertyName=[Qty] Qty=[100]

which means the first property changed event of the proxy is called when the underlying value is not changed.

why is that? Am i doing something wrong?

Upvotes: 1

Views: 447

Answers (1)

Jan Muncinsky
Jan Muncinsky

Reputation: 4408

Problem is how Castle intercepts methods with ref parameters. I believe that underneath generated proxy class looks something like:

public class DummyViewModelProxy : DummyViewModel
{
    protected override bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        var interceptor = new DummyInterceptor();
        IInvocation invocation = new Invocation(storage, value, propertyName);
        interceptor.Intercept(invocation);
        storage = (T)invocation.Arguments[0];
        return (bool)invocation.ReturnValue;
    }

    ....
}

Then the chain of ref parameters is broken and value is really set after PropertyChanged being called.

Upvotes: 1

Related Questions