Hi,
I have a situation where I need to clear the event sinks from an event. But
let's say I don't know which methods (by name, I mean) signed up for that
event.
I do have a control with lets say two events: ItemCheck and
DataSourceChange. Callbacks assigned to ItemCheck and DataSourceChange
events at designtime.
The logic within DataSourceChanged callback requires temporary to disable
the ItemCheck which can be accomplished by smth. Like this:
private void controlX_DataSourceChanged(object sender, System.EventArgs e)
{
this.controlX.ItemCheck -= new
ItemCheckEventHandler(this.controlX_ItemCheck);
try
{
....
}
finally
{
this.controlX.ItemCheck += new
ItemCheckEventHandler(this.controlX_ItemCheck);
}
}
Now, the question is how to do the same thing without explicitly specifying
this.controlX_ItemCheck name? I'm looking for something like I was able to
do in Delphi, for example,
1) store the existing one to the local variable
2) assign this one to null
3) do some work here...
4) restore back to the one stored into local variable
I believe you know what I'm talking about.
I know I have to use reflection in .NET/C# in order to get this thing done,
but how?
P.S. Such things does not help because I have to specify the name of the
callback explicitly as well:
// EventInfo ei = (sender as MyControl).GetType().GetEvent("ItemCheck");
// Delegate d = Delegate.CreateDelegate(ei.EventHandlerType, this,
"controlX_ItemCheck");
ei.RemoveEventHandler(sender, d);
try
{
...
}
finally
{
ei.AddEventHandler(sender, d);
}
Thanks in advance,
Serge