public void TestReactiveMethod() { MethodInfo method = typeof(DateTime) .GetProperty(nameof(DateTime.Now), BindingFlags.Static | BindingFlags.Public) .GetGetMethod(); int count = 0; DateTime bday = new DateTime(1955, 10, 28); // Note: Observing this test through the debugger will call DateTime.Now, // thus incrementing 'count', and breaking the test. Watch out. using (Redirection.Observe(method) .Where(_ => count++ % 2 == 0) .Subscribe(ctx => ctx.ReturnValue = bday)) { DateTime.Now.ShouldBe(bday); DateTime.Now.ShouldNotBe(bday); DateTime.Now.ShouldBe(bday); DateTime.Now.ShouldNotBe(bday); } DateTime.Now.ShouldNotBe(bday); DateTime.Now.ShouldNotBe(bday); count.ShouldBe(4); }
static RxApp() { var methodInfos = typeof(XafApplication).Methods(); CreateControllersOptimized = methodInfos.Where(info => info.Name == nameof(CreateControllers) && info.Parameters().Count == 4).Select(info => info.DelegateForCallMethod()).First(); CreateControllers = methodInfos.Where(info => info.Name == nameof(CreateControllers) && info.Parameters().Count == 3).Select(info => info.DelegateForCallMethod()).First(); CreateWindowCore = methodInfos.First(info => info.Name == nameof(CreateWindowCore)).DelegateForCallMethod(); OnPopupWindowCreated = Redirection.Observe(methodInfos.First(info => info.Name == nameof(OnPopupWindowCreated))) .Publish().RefCount(); var createNestedFrame = methodInfos.First(info => info.Name == nameof(XafApplication.CreateNestedFrame)); NestedFrameRedirection = Redirection.Observe(createNestedFrame) .Publish().RefCount(); }
public void TestBuiltInObserver() { DateTime birthday = new DateTime(1955, 10, 28); MethodInfo method = typeof(DateTime) .GetProperty(nameof(DateTime.Now), BindingFlags.Static | BindingFlags.Public) .GetGetMethod(); DateTime.Now.ShouldNotBe(birthday); using (Redirection.Observe(method, ctx => ctx.ReturnValue = birthday)) { DateTime.Now.ShouldBe(birthday); } DateTime.Now.ShouldNotBe(birthday); }
public void TestInstanceRedirection() { MethodInfo method = typeof(TestClass) .GetMethod(nameof(TestClass.ComputeHash), BindingFlags.Instance | BindingFlags.Public); const int SEED = 0xEA6C23; TestClass test = new TestClass(SEED); string testStr = "42"; int testHash = testStr.GetHashCode(); test.ComputeHash(testStr).ShouldBe(unchecked (testHash * SEED)); test.ComputeHash(testStr).ShouldNotBe(SEED); using (Redirection.Observe(method, ctx => ctx.ReturnValue = ((TestClass)ctx.Sender).Seed)) { test.ComputeHash(testStr).ShouldBe(SEED); } test.ComputeHash(testStr).ShouldBe(unchecked (testHash * SEED)); }