Trying to refactor this code and hit a wall. [resolved]
Here is a sample of the code I am refactoring it works great as is, but I don't want to write (copy/paste/change) this code for every time I need it.
VB Code:
Public Sub RaiseSendMessageEvent(ByVal e As DocGenCommon.DocGen.DocGenEventArgs)
For Each del As [Delegate] In SendMessageEvent.GetInvocationList()
Try
del.Method.Invoke(del.Target, New Object() {e})
Catch
SendMessageEvent = DirectCast(System.Delegate.Remove(SendMessageEvent, del), IDocGenEventServer.SendMessageHandler)
End Try
Next
End Sub
So far I have gotten this far and my problem is I cannot pass in the Type to the directcast statement.
VB Code:
Public Sub RaiseRemotedEvent(ByVal sender As [Delegate], ByVal eventtype As Type, ByVal e As DocGenCommon.DocGen.DocGenEventArgs)
For Each del As [Delegate] In sender.GetInvocationList()
Try
del.Method.Invoke(del.Target, New Object() {e})
Catch
' this is where the error is "Type 'eventtype' is not defined."
sender = DirectCast(System.Delegate.Remove(sender, del), eventtype)
End Try
Next
End Sub
Any suggestions?
Re: Trying to refactor this code and hit a wall.
try ...., eventtype.gettype) instead...
that parameter is expecting a type not a variable name.
Re: Trying to refactor this code and hit a wall.
Quote:
Originally Posted by Andy
try ...., eventtype.gettype) instead...
that parameter is expecting a type not a variable name.
:) Tried that.
I've tried eventtype.GetType, GetType(eventtype), passing an object declaring a type and using GetType. So far nothing works.
I should rewrite this in C# and see if it complains.
Re: Trying to refactor this code and hit a wall.
It appears to work if I use this:
VB Code:
sender = System.Delegate.Remove(sender, del)
I will have to do more testing to make sure it works right.
Re: Trying to refactor this code and hit a wall.
This is what I found that worked.
VB Code:
Public Sub RaiseRemotedEvent(ByVal sender As [Delegate])
RaiseRemotedEvent(sender, New Object() {})
End Sub
Public Sub RaiseRemotedEvent(ByVal sender As [Delegate], ByVal args As Object())
For Each del As [Delegate] In sender.GetInvocationList()
Try
del.Method.Invoke(del.Target, args)
Catch
sender = System.Delegate.Remove(sender, del)
End Try
Next
End Sub