Beispiel #1
0
      public void InterceptCallToPropertyInNonInterceptedMethod()
      {
         bool wasIntercepted = false;

         MyInterceptor interceptor = new MyInterceptor();
         interceptor.Intercepted += (s, e) =>
         {
            wasIntercepted = true;
         };
         Person p = new Person { Name = "John" };
         p.SetAgeTo(50);
         p = Decorate(p, interceptor);

         p.SetAgeTo(100);

         var originalObject = p.GetType().GetField("__target").GetValue(p);
         var ageInOriginalObject = originalObject.GetType().GetProperty("Age", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(originalObject, null);

         Assert.That(ageInOriginalObject, Is.EqualTo(100)); // The value is still 50
         Assert.That(wasIntercepted, Is.True); // The call was not intercepted
      }