-
Apr 7th, 2014, 07:50 PM
#1
[RESOLVED] GetInvocationList from outside the class with the event?
Just say I have for eg a given printdocument ... and I want to get all attached Invokes on this, how would I go about doing this since the GetInvocationList seems to only be available from within the class that has the event (eg printdocument in this case).
Thanks,
Kris
-
Apr 7th, 2014, 10:10 PM
#2
Re: GetInvocationList from outside the class with the event?
I think that you will have to Reflect on this a bit.
vb.net Code:
Public Class Form1
Private WithEvents pd As New Printing.PrintDocument
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fieldinfos() As Reflection.FieldInfo = GetType(Printing.PrintDocument).GetFields(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
' Events ref: [url]http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument_events%28v=vs.110%29.aspx[/url]
Dim PrintEventHandlers As IEnumerable(Of Reflection.FieldInfo) _
= From fi In fieldinfos _
Where fi.FieldType Is GetType(System.Drawing.Printing.PrintEventHandler) _
Select fi
Dim PrintPageEventHandlers As IEnumerable(Of Reflection.FieldInfo) _
= From fi In fieldinfos _
Where fi.FieldType Is GetType(System.Drawing.Printing.PrintPageEventHandler) _
Select fi
Dim QueryPageSettingsEventHandlers As IEnumerable(Of Reflection.FieldInfo) _
= From fi In fieldinfos _
Where fi.FieldType Is GetType(System.Drawing.Printing.QueryPageSettingsEventHandler) _
Select fi
For Each fi As Reflection.FieldInfo In PrintEventHandlers
Dim mcd As MulticastDelegate = DirectCast(fi.GetValue(pd), MulticastDelegate)
Dim dels() As [Delegate] = mcd.GetInvocationList
Next
End Sub
Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.BeginPrint
' FieldType = {Name = "PrintEventHandler" FullName = "System.Drawing.Printing.PrintEventHandler"}
End Sub
Private Sub pd_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.EndPrint
' FieldType = {Name = "PrintEventHandler" FullName = "System.Drawing.Printing.PrintEventHandler"}
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
' FieldType = {Name = "PrintPageEventHandler" FullName = "System.Drawing.Printing.PrintPageEventHandler"}
End Sub
Private Sub pd_QueryPageSettings(ByVal sender As Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles pd.QueryPageSettings
' FieldType = {Name = "QueryPageSettingsEventHandler" FullName = "System.Drawing.Printing.QueryPageSettingsEventHandler"}
End Sub
End Class
If there are no handlers attached, you will not retrieve any fieldinfo for the given type with this code. It is probably set to Nothing.
-
Apr 10th, 2014, 06:54 AM
#3
Re: GetInvocationList from outside the class with the event?
Thanks a lot ... btw here is my final implementation of your code:
vb Code:
Public Shared Function GetAttachedEvents(Of T)([Object] As Object, Optional EventPrivateName As String = "", Optional BaseType As Type = Nothing) As List(Of T)
GetAttachedEvents = New List(Of T)
If [Object] IsNot Nothing Then
If BaseType Is Nothing Then
BaseType = [Object].GetType
End If
Dim fieldinfos = BaseType.GetFields(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
' Events ref: [url]http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument_events%28v=vs.110%29.aspx[/url]
Dim EventHandlers As IEnumerable(Of Reflection.FieldInfo) _
= From fi In fieldinfos _
Where fi.FieldType Is GetType(T) AndAlso (EventPrivateName = "" OrElse LCase(fi.Name) = LCase(EventPrivateName)) _
Select fi
'AndAlso (EventPrivateName = "" OrElse LCase(fi.FieldType.FullName) = LCase(EventPrivateName) OrElse fi.FieldType.FullName.EndsWith("." & EventPrivateName, StringComparison.OrdinalIgnoreCase))
For Each fi As Reflection.FieldInfo In EventHandlers
Dim mcd As MulticastDelegate = DirectCast(fi.GetValue([Object]), MulticastDelegate)
Dim dels() As [Delegate] = mcd.GetInvocationList
GetAttachedEvents.AddRange(dels.OfType(Of T)().ToArray)
Next
End If
End Function
Thanks again,
Kris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|