在旧计算机上运行的ReactiveExtension现在失败

时间:2016-01-14 20:48:21

标签: c# system.reactive

我正在使用Weak Events in .Net, the easy way中的代码来处理对可观察集合的监视更改。几个月来,代码一直没有任何问题。我最近更新到一台新电脑。在完成所有设置并从我的存储库中下载代码后,我遇到了一个奇怪的问题。代码不再有效!

这是我的代码的相关部分,它都发生在构造函数中:

 public class PurchaseOrderReceipt : BaseEntity
{
    /// <summary>
    ///     Initializes a new instance of the <see cref="PurchaseOrderReceipt" /> class.
    /// </summary>
    public PurchaseOrderReceipt()
    {
        this.ReceiptItems = new ObservableCollection<PurchaseOrderItemReceipt>();
        this.DateReceived = DateTime.Now;

        this.ReceiptItems.ObserveCollectionChanged()
            .SubscribeWeakly(this, (target, eventArgs) => target.ReceiptItemsChanged());
    }

在SubscribeWeakly行上抛出异常,并显示以下错误消息: ArgumentException:onNext必须引用静态方法,否则订阅仍将保留对目标的强引用

我可以通过创建PurchaseOrderReceipt的实例来重新创建LinqPad中的问题。

如果我在LinqPad中编写一个简单的类,它反映了PurchaseOrderReceipt类中的设置而不是它的工作原理。

LinqPad代码:

void Main()
{
    var x = new Test(); 
    x.ReceiptItems.Add(new PurchaseOrderItemReceipt());

}

public class Test:BaseEntity
{
    public ObservableCollection<PurchaseOrderItemReceipt> ReceiptItems {get; set;}

    public Test()
    {
        this.ReceiptItems = new ObservableCollection<PurchaseOrderItemReceipt>();
        this.ReceiptItems.ObserveCollectionChanged().SubscribeWeakly(this,(target, eventargs) => target.TestChanged());
    }

    private void TestChanged()
    {
        "Changed!".Dump();
    }
}

改变了!在结果窗口中打印出来。

这是来自顶部链接的CustomReactiveExtension类。

public static class CustomReactiveExtension
    {
        public static IObservable<EventPattern<NotifyCollectionChangedEventArgs>> ObserveCollectionChanged(this INotifyCollectionChanged collection)
        {
            return Observable.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
                handler => (sender, e) => handler(sender, e),
                handler => collection.CollectionChanged += handler,
                handler => collection.CollectionChanged -= handler);
        }

        public static IDisposable SubscribeWeakly<T, TTarget>(this IObservable<T> observable, TTarget target, Action<TTarget, T> onNext) where TTarget : class
        {
            var reference = new WeakReference(target);

            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");
            }

            IDisposable subscription = null;
            subscription = observable.Subscribe(item =>
            {
                var currentTarget = reference.Target as TTarget;
                if (currentTarget != null)
                {
                    onNext(currentTarget, item);
                }
                else
                {
                    subscription.Dispose();
                }
            });

            return subscription;
        }
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我不是100%肯定,但我的猜测是编译器的不同版本或不同的编译选项导致你的lambda被编译为实例方法而不是静态方法。

最简单的解决方案是明确实现一个静态方法用作onNext回调,即:

private static void OnReceiptItemsChanged(PurchaseOrderReceipt target, 
    EventPattern<NotifyCollectionChangedEventArgs> eventPattern)
{
    // TODO Do something here
}

然后像这样使用SubscribeWeakly

this.ReceiptItems.ObserveCollectionChanged().SubscribeWeakly(this, OnReceiptItemsChanged);

现在无论您使用哪种编译器或使用哪种编译选项,回调始终都是静态方法。