1

読み取り専用プロパティを設定するためにObservableAsPropertyHelperを使用しようとしていますが、何を試しても期待どおりに機能しないようです。

1つのテストにまとめることができます(ReactiveUI 4.3.2.0、Nunit.Framework、Shouldを使用、すべて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();
    }

しかし、これは結果として

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

パラメータがに渡されていることを確認できます

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

そして、GitHubのソースを調べた後、

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

私のコードは例外を回避しますが、テストにも失敗します。

どうすればTMが間違っているのを避けることができますか?

4

1 に答える 1

2

_Updatedは実際には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();
    }
于 2013-02-01T15:01:31.070 に答える