ReactiveUI ObservableAsPropertyHelper throws Cannot be converted to type exception

Tag: reactiveui Author: xtkxlp Date: 2013-01-16

I'm trying to use ObservableAsPropertyHelper in order to set a readonly Property, but no matter what I try I can't seem to get it working as I expect.

I can distill it into a single test (using ReactiveUI 4.3.2.0, Nunit.Framework and Should, all from NuGet)

[TestFixture]
public class ObservableAsPropertyHelperTests : ReactiveObject 
{
    private bool _Updated;

    public bool Updated
    {
        get { return _Updated; }
        set { this.RaiseAndSetIfChanged(x => x.Updated, value); }
    }

    [Test]
    public void ShouldSetProperty()
    {
        var input = new Subject<bool>();
        var propertyHelper = input.ToProperty(
                source: this, 
                property: x => x.Updated);//Exception here 

        input.OnNext(true);

        this.Updated.ShouldBeTrue();
    }

but this results in

System.ArgumentException : Object of type 'ReactiveUI.ObservableAsPropertyHelper`1[System.Boolean]' cannot be converted to type 'System.Boolean'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.RtFieldInfo.InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, Boolean doVisibilityCheck, Boolean doCheckConsistency)
   at System.Reflection.RtFieldInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
   at ReactiveUI.OAPHCreationHelperMixin.ToProperty[TObj,TRet](IObservable`1 This, TObj source, Expression`1 property, TRet initialValue, IScheduler scheduler, Boolean setViaReflection) in y:\Dropbox\ReactiveUI_External\ReactiveUI\ObservableAsPropertyHelper.cs:line 184#0
   at RxUILearning.ObservableAsPropertyHelperTests.ShouldSetProperty() in C:\Dev\RxUILearning\ObservableAsPropertyHelperTests.cs:line 45#1

I can verify the paramaters being passed in to

    //   source:
    //     The ReactiveObject that has the property
    //
    //   property:
    //     An Expression representing the property (i.e.  'x => x.SomeProperty'

And after looking through the source on GitHub I can see that by calling

var propertyHelper = input.ToProperty(source: this, property: x => x.Updated, setViaReflection:false);

my code avoids the Exception, but also fails the test.

How can I avoid Doing It WrongTM?

Best Answer

_Updated should actually be an ObservableAspropertyHelper

[TestFixture]
public class ObservableAsPropertyHelperTests : ReactiveObject 
{
    private ObservableAsPropertyHelper<bool> _Updated;

    public bool Updated
    {
        get { return _Updated.Value; }
    }

    [Test]
    public void ShouldSetProperty()
    {
        var input = new Subject<bool>();
        input.ToProperty(
                source: this, 
                property: x => x.Updated);//Now should work

        input.OnNext(true);

        this.Updated.ShouldBeTrue();
    }

comments:

I knew it had to be something simple. Thanks!