简体   繁体   English

观察继承的 class 的 ReactiveObject.Changed 可能有效也可能无效

[英]Observe ReactiveObject.Changed of inherited class may or may not work

I try to create a base class, which inherits from ReactiveObject.我尝试创建一个继承自 ReactiveObject 的基础 class。 This class needs to check if any property, including properties of further inheritations, has changed.此 class 需要检查是否有任何属性(包括进一步继承的属性)已更改。

My main problem is, that I can't get any reliable testresults.我的主要问题是,我无法获得任何可靠的测试结果。 The provided test MAY be green, Running it with VS Live Unit Testing.提供的测试可能是绿色的,使用 VS Live Unit Testing 运行它。 brings it in red.把它变成红色。 Moving the classes out of the testproject into a library may or may not get this test green.将测试项目中的类移到库中可能会也可能不会使这个测试变绿。

So far, the only way to keep it green, is to uncomment this line in the inherited class.到目前为止,保持它绿色的唯一方法是在继承的 class 中取消注释此行。

//  Changed.Subscribe(Console.WriteLine);
   [TestClass]
   public class StateBaseTests
   {
      #region Tests

      [TestMethod]
      public void State_ChangesToChanged_PropertyChanged()
      {
         var _item = new MyTest();

         _item.Surname = "Testname";

         Assert.AreEqual(Statetype.Changed, _item.State);
      }

      #endregion

      #region SubClasses

      public enum Statetype
      {
         Added,
         Changed,
         Deleted,
         Detached,
         Unchanged
      }

      public abstract class StateBase : ReactiveObject
      {
         protected StateBase()
         {
            State = Statetype.Unchanged;

            Changed.Select(prop => prop.PropertyName)
                   .Subscribe(UpdateZustand);
         }

         protected void UpdateZustand(string propertyName)
         {
            if(State == Statetype.Unchanged)
               State = Statetype.Changed;
         }

         [Reactive]
         public Statetype State { get; set; }
      }

      private class MyTest : StateBase
      {
         public MyTest()
         {
            //  Changed.Subscribe(Console.WriteLine);
         }

         [Reactive]
         public string Name { get; set; }

         [Reactive]
         public string Surname { get; set; }
      }
   }

Here is a slight adaptation of your sample.这是您的样本的轻微改编。 Did it using VS Mac Community & NUnit.使用 VS Mac Community 和 NUnit 完成的。 The test does turn green every time on my machine.每次在我的机器上测试都会变成绿色。 As you can see the property name is also printed.如您所见,还打印了属性名称。 On my console it outputs:在我的控制台上它输出:

State
Surname

Here is the code:这是代码:

using System;
using System.Reactive.Linq;
using NUnit.Framework;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;

namespace Testy {
    [TestFixture]
    public class Test {
        [Test]
        public void State_ChangesToChanged_PropertyChanged() {
            var _item = new MyTest();
            _item.Surname = "Testname";
            Assert.AreEqual(Statetype.Changed, _item.State);
        }
    }

    public enum Statetype {
        Added,
        Changed,
        Deleted,
        Detached,
        Unchanged
    }

    public abstract class StateBase : ReactiveObject {
        protected StateBase() {
            State = Statetype.Unchanged;

            Changed
                .Select(prop => prop.PropertyName)
                .Subscribe(
                    name => {
                        UpdateZustand(name);
                        Console.WriteLine(name);
                    });
        }

        protected void UpdateZustand(string propertyName) {
            if (State == Statetype.Unchanged)
                State = Statetype.Changed;
        }

        [Reactive]
        public Statetype State { get; set; }
    }

    public class MyTest : StateBase {
        [Reactive]
        public string Name { get; set; }

        [Reactive]
        public string Surname { get; set; }
    }
}

Not sure if this helps, but it it was fun to try it out:).不确定这是否有帮助,但尝试一下很有趣:)。

PS: These are the used package versions: PS:这些是使用的 package 版本:

  • ReactiveUI(.Fody) 10.5.1 ReactiveUI(.Fody) 10.5.1
  • Nunit 3.12.0 Nunit 3.12.0
  • NUnit3TestAdapter 3.15.1 NUnit3TestAdapter 3.15.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何指定可能包含另一个类的类 - How to specify a class that may contain another class 访问可能不可用的第三方团体 - Access 3rd Party Class That May Not Be Available 引用两个程序集之一中可能存在的类 - Referencing a class that may exist in one of two assemblies 如何创建可能返回 null 的 class? - How to create a class that may return null? 如何添加一个属性来表示样品名称,一旦初始化就不能更改? - How to add a property to represent a sample name that may not be changed once initialized? 没有 ID 为 1 的文件。文件列表可能已更改 Blazor - There is no file with ID 1. The file list may have changed Blazor 声明“可空<string> []" 或 "string[]?" 对于 class 中可能存在也可能不存在的字符串数组属性?</string> - Declare "Nullable<string>[]" or "string[]?" for string array property that may or may not exist inside a class? 如果DLL可用或不可用,如何扩展C#ASP.NET类? - How to extend a C# ASP.NET class if DLL may or may not be available? 接口实现两次“类型可以统一”; 为什么这个解决方法有效? - Interface implemented twice “types may unify”; why does this workaround work? 如何创建可以使用两个名称空间之一的类? - How to create a class that may use one of two namespaces?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM