I have a probably simple issue but my knowledge doesn't cover up why I got a different behavior with the following 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
}
I got the abstract base class AbstractTestCase where the property TestResult is beeing changed while runtime.
The initial value is "Undefined" whose change should not raise the ResultStateChangedEventHandler. This is ok so far.
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.
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 AbstractTestCase class type.
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".
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.
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 tc-variable.
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.
So where's my mistake? And if reflection is slower, why is my cast even slower than the slow reflection? Thank you :)
Aucun commentaire:
Enregistrer un commentaire