Ticket CQ66908
Visible to All Users

How do you use reflection to get the invocation list of an event delegate in a BarButtonItem?

created 18 years ago

I need to have an event attached to all of my BarButtonItems ItemClick event that will get invoked before any other events that may be placed on that form. I wanted to do this in code at runtime so that I wouldn't have to subclass the BarButtonItem.
With a BarButtonItem, I am getting stuck right at the first part, getting the invocation list of the delegate…
Here is a sample of what I have done before:
Dim ItemClickDelegate As [Delegate] = CType(GetType(DevExpress.XtraBars.BarButtonItem).GetField("ItemClick", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public).GetValue(ctrl), [Delegate])
Dim invocationList As [Delegate]() = ItemClickDelegate.GetInvocationList()
However, it seems that a field for the delegate is not even available. A GetFields() call on the type shows that there are no fields at all…
Upon doing some searching, I found out that the field is not always created and that it will depend on how the code for that event is written…
Is there a way to get the invocation list or is it simply not going to be available with the BarButtonItem?

Answers

created 18 years ago

Hi Aaron,
I've redesigned your code a bit : You should call the "itemClick" member (its case sensitive) instead, you should use also others BindingFlags in your code.
For example:
Private Sub Form1_Load(sender As Object, e As EventArgs)
   Dim o As Object = GetType(DevExpress.XtraBars.BarItem).GetField("itemClick", BindingFlags.NonPublic Or BindingFlags.Static).GetValue(Nothing)
   Dim ehl As EventHandlerList = barButtonItem1.GetType().BaseType.GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(barButtonItem1, Nothing) '
   Dim iche As New ItemClickEventHandler(MyItemClickHandler)
   ehl.AddHandler(o, iche)
 'ehl(o);
End Sub 'Form1_Load
Private Sub MyItemClickHandler(sender As Object, e As ItemClickEventArgs)
   MessageBox.Show("Hit")
End Sub 'MyItemClickHandler
Thanks,
Andrew

    Comments (2)

      Hi,
      I need something similar. I want to remove all CheckedChanged events attached to a CheckEdit control at runtime. For this, I need the delegates of the event in an CheckEdit object.  I tried to get them getting fileds with "GetField" and after with "GetInvocationList". I tried too with an EventHandlerList but I don't find it neither.
      Anyway to get them? Thanks in advance!

      Sasha (DevExpress Support) 8 years ago

        Hello David,
        To avoid mixing several questions in one thread, I've created a separate ticket on your behalf: CheckEdit - How to remove all CheckedChanged event handlers using Reflection.
        I will address it shortly.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.