title: Eventhandler: Sender-cast vs. reflection
body: <p>I have a probably simple issue but my knowledge doesn't cover up why I got a different behavior with the following code:</p>
  
  <pre><code>private void Test_ResultStateChangedEventHandler(object sender, EventArgs e)
  {
      Testcases.AbstractTestCase tc = (Testcases.AbstractTestCase)sender;
  
      System.Reflection.MethodInfo mInfo = sender.GetType().BaseType.GetMethod("get_TestResult");
  
      object retval = mInfo.Invoke(sender, new object[] { });
  
      if (retval != tc.TestResult) 
          throw new ArgumentException(); // Just to get a hold
  }
  </code></pre>
  
  <p>I got the abstract base class <em>AbstractTestCase</em> where the property <em>TestResult</em> is beeing changed while runtime.</p>
  
  <p>The initial value is "Undefined" whose change should not raise the ResultStateChangedEventHandler. This is ok so far. </p>
  
  <p>If I change the result state then I expect it to be "not_tested". Immediately after the test start (which follows directly after the initialize that sets the value to "not_tested") this property is set to "running" causing the event to fire.</p>
  
  <p>Now I got two different results depending on whether I access this abstact class via reflection or if I simpyl cast my sender to my <em>AbstractTestCase</em> class type.</p>
  
  <p>While reflection delivers "not_tested" as expected, the direct cast will deliver "running" even though the single step with the debugger is also saying "not_tested".</p>
  
  <p>It's to mention, that the testcase is running in a different thread and therefore I suppose that the value has already been changed before I access it's property.</p>
  
  <p>The reason this confuses me, is because I expected reflection as beeing way more slow than a instance reference that I create by casting the sender to my <em>tc</em>-variable.</p>
  
  <p>I'd prefer to use the cast instead of the reflection because it's a whole bunch of code to simply access the class members.</p>
  
  <p>So where's my mistake? And if reflection is slower, why is my cast even slower than the slow reflection? Thank you :)</p>
  
  <h1>Edit 1: Some more code</h1>
  
  <p><em>_tcMngr</em> is nothing more than a collection of classes allowing to dynamically run the different testcases and is fed with the dummy class. Eventhandler and the code below is running in UI Thread. <em>AbstractTestCase</em> is running in a new thread.</p>
  
  <p>In the UI thread there's beeing started the test:</p>
  
  <pre><code>_tcMngr.Initialize(identifier);
  
  var t = Task.Factory.StartNew(() =&gt;
  {
      _tcMngr.RunSingle(identifier);
  });
  </code></pre>
  
  <p>This is the property that is set while running:</p>
  
  <pre><code>public abstract class AbstractTestCase : TestReport, IStdTestCase
  {
      public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);
  
      public event StatusChangedEventHandler StatusChanged;
  
      protected virtual void OnStatusChanged(StatusChangedEventArgs e)
      {
          if (StatusChanged != null)
              StatusChanged(this, e);
      }
  
      protected virtual void ReportProgress(int percentage, string step, int maxPercentage = 100)
      {
          OnStatusChanged(new StatusChangedEventArgs()
          {
              Percentage = percentage,
              Step = step,
              MaxPercentage = maxPercentage               
          });
      }
  
      public class StatusChangedEventArgs : EventArgs
      {
          public int Percentage { get; set; } = 0;
          public int MaxPercentage { get; set; } = 100;
          public string Step { get; set; } = "";
      }
  
      public abstract void Run();
      public abstract void Dispose();
      public abstract string ShortDescription {
          get;
      }
  
      public virtual int EstimatedRuntime { get; } = 100;
  
      public abstract override string ToString();
      public abstract void Initialize();
  
      public int Identifier { get; set; }
  }
  
  public abstract class TestReport {
      public enum TestCaseResultStates
      {
          Running = 2,
          Success = 1,
          Fail = 0,
          Undefined = -1,
          NotTested = int.MaxValue
      }
  
  
      TestCaseResultStates _testresult = TestCaseResultStates.Undefined;
  
      public TestCaseResultStates TestResult
      {
          get
          {
              return _testresult;
          }
  
          set
          {
              _testresult = value;
  
              OnResultStateChanged(null);
          }
      }
  }
  </code></pre>
  
  <p>And this is a dummy test case for debugging:</p>
  
  <pre><code>public class Dummy : AbstractTestCase
  {
      public override string ShortDescription
      {
          get
          {
              return "Dummy for debugging.";
          }
      }
  
      public override void Dispose()
      {
          // nothing to do here
      }
  
      public override void Initialize()
      {
          base.TestResult = TestCaseResultStates.NotTested;
      }
  
      public override void Run()
      {
          base.TestResult = TestCaseResultStates.Running;
  
          for (int i = 1; i &lt; 11; i++)
          {
              System.Threading.Thread.Sleep(1000);
  
              base.ReportProgress(i * 10, "Step " + i + " / 10");
          }
  
          base.TestResult = TestCaseResultStates.Fail;
          base.TextResult = "Test was finished: \"" + base.TestResult.ToString() + "\"";
      }
  
      public override string ToString()
      {
          return "Dummy";
      }
  
      public override int EstimatedRuntime
      {
          get
          {
              return 10;
          }
      }
  }
  </code></pre>