I was working with my Reflector class when I ran into a little problem, and after a couple of hours of debugging and toying around and researching I can't seem to make any progress.
What I am trying to do is access a private field (dialogResult) on an instance of a class that derives from Windows.Forms.Form. I found it very interesting that I could access the dialogResult field from a System.Type that refers to System.Windows.Forms.Form, but not a System.Type that refers to the actual runtime type of my derived form. I found it even more interesting that while I could access the _message field of an exception with a System.Type that refers to System.Exception, I could also access it with a System.Type that refers to the derived System.UriFormatException. How very inconsistent...
The code I was using was the following:
The reason that I used UriFormatException is because it is defined in a different assembly than System.Exception (I thought that the issue might have had to do with the fact that System.Windows.Forms.Form is defined in a different assembly than my form class).
I am stumped. Can anyone think of anything?
What I am trying to do is access a private field (dialogResult) on an instance of a class that derives from Windows.Forms.Form. I found it very interesting that I could access the dialogResult field from a System.Type that refers to System.Windows.Forms.Form, but not a System.Type that refers to the actual runtime type of my derived form. I found it even more interesting that while I could access the _message field of an exception with a System.Type that refers to System.Exception, I could also access it with a System.Type that refers to the derived System.UriFormatException. How very inconsistent...
The code I was using was the following:
Code:
[/color][Color=DarkRed]Exception [/Color]ex = [Color=Blue]new [/Color][Color=DarkRed]UriFormatException[/Color]("Moo");
[Color=DarkRed]Reflector [/Color]Rex = [Color=Blue]new [/Color][Color=DarkRed]Reflector[/Color](ex);
Rex.PrivateAccess = [Color=Blue]true[/Color];
[Color=DarkRed]MessageBox[/Color].Show(Rex.GetField("_Message").ToString());
[Color=DarkRed]Reflector [/Color]Me = [Color=Blue]new [/Color][Color=DarkRed]Reflector[/Color]([Color=Blue]this[/Color]);
Me.IgnoreCase = [Color=Blue]false[/Color];
Me.PrivateAccess = [Color=Blue]true[/Color];
[Color=DarkRed]MessageBox[/Color].Show(Me.GetField("dialogResult").ToString());
[Color=Green]// I also tried the following InvokeMember equivalent and got the same result:
[/Color][Color=DarkRed]Exception [/Color]ex = [Color=Blue]new [/Color][Color=DarkRed]UriFormatException[/Color]("Moo");
[Color=Blue]object [/Color]exMessage = ex.GetType().InvokeMember(
"_message",
[Color=DarkRed]System.Reflection.BindingFlags[/Color].Instance |
[Color=DarkRed]System.Reflection.BindingFlags[/Color].FlattenHierarchy |
[Color=DarkRed]System.Reflection.BindingFlags[/Color].NonPublic |
[Color=DarkRed]System.Reflection.BindingFlags[/Color].GetField,
[Color=Blue]null[/Color], ex, [Color=Blue]null[/Color]);
[Color=DarkRed]MessageBox[/Color].Show(exMessage.ToString());
[Color=Blue]object [/Color]thisDialogResult = [Color=Blue]this[/Color].GetType().InvokeMember(
"dialogResult",
[Color=DarkRed]System.Reflection.BindingFlags[/Color].Instance |
[Color=DarkRed]System.Reflection.BindingFlags[/Color].FlattenHierarchy |
[Color=DarkRed]System.Reflection.BindingFlags[/Color].NonPublic |
[Color=DarkRed]System.Reflection.BindingFlags[/Color].GetField,
[Color=Blue]null[/Color],
this, [Color=Blue]null[/Color]);
[Color=DarkRed]MessageBox[/Color].Show(thisDialogResult.ToString());
The reason that I used UriFormatException is because it is defined in a different assembly than System.Exception (I thought that the issue might have had to do with the fact that System.Windows.Forms.Form is defined in a different assembly than my form class).
I am stumped. Can anyone think of anything?