W
Wiktor Zychla
Let's start with an example: if you have:
class Test
{
public delegate int MyDelegate( int n );
event MyDelegate MyEvent;
}
then you can get MyEvent using reflection:
Test test = new Test(...);
Type t = typeof( Test );
t.InvokeMember( "MyEvent", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, test, null );
this works like a charm, you get reference to the delegate of type
MyDelegate.
Then comes the problem: why following does not work:
class MyForm : Form
{
Button b;
...
}
Type t = typeof( Button );
t.InvokeMember( "Click", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, b, null );
this example looks like a perfect copy of above one. Click event is declared
as field in Control class and thus, according to my intuition, should be
available via reflection. I would expect to get a delegate of type
EventHandler. instead an exception of missing member is thrown.
I just hope I miss something obvious. If not, I really would like to hear a
technical explanation, can be complicated, I am not afraid [I know how
delegates and events work at IL level].
Thanks in advance,
Wiktor Zychla
class Test
{
public delegate int MyDelegate( int n );
event MyDelegate MyEvent;
}
then you can get MyEvent using reflection:
Test test = new Test(...);
Type t = typeof( Test );
t.InvokeMember( "MyEvent", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, test, null );
this works like a charm, you get reference to the delegate of type
MyDelegate.
Then comes the problem: why following does not work:
class MyForm : Form
{
Button b;
...
}
Type t = typeof( Button );
t.InvokeMember( "Click", BindingFlags.Instance | BindingFlags.GetField |
BindingFlags.NonPublic, null, b, null );
this example looks like a perfect copy of above one. Click event is declared
as field in Control class and thus, according to my intuition, should be
available via reflection. I would expect to get a delegate of type
EventHandler. instead an exception of missing member is thrown.
I just hope I miss something obvious. If not, I really would like to hear a
technical explanation, can be complicated, I am not afraid [I know how
delegates and events work at IL level].
Thanks in advance,
Wiktor Zychla