Home All Groups Group Topic Archive Search About

Retrieving a handle to a private delegate

Author
27 Feb 2006 3:10 PM
Fredrik Strandberg
Hi!

I have some problems when testing a system.

In class A i have:

Public Event CommEvent As CommEventHandler
Public Delegate Sub CommEventHandler()

and a method:

EnableEvents(ByVal Callback As CommEventHandler)

Then in class B i have

Private Sub OnCommEventReceived() Handles A.CommEvent

>From my test class (class C) I need to call EnableEvents with
OnCommEventReceived as an argument. The problem is I do not know how to
provide a handle to OnCommEventReceived. Since OnCommEventReceived is
private I guess I need to use reflection, but I cannot make it work.

I have used for instance MethodInfo to invoke private methods before,
but now I just need some kind of handle to the OnCommEventReceived
method in class B to provide to the EnableEvents method in class A.

Thank you!

/Fredrik Strandberg

Author
27 Feb 2006 3:31 PM
Fredrik Strandberg
The "addressof" operator must be used somehow or am I on the wrong
track?
Author
28 Feb 2006 10:57 AM
Larry Lard
Fredrik Strandberg wrote:
Show quoteHide quote
> Hi!
>
> I have some problems when testing a system.
>
> In class A i have:
>
> Public Event CommEvent As CommEventHandler
> Public Delegate Sub CommEventHandler()
>
> and a method:
>
> EnableEvents(ByVal Callback As CommEventHandler)
>
> Then in class B i have
>
> Private Sub OnCommEventReceived() Handles A.CommEvent
>
> >From my test class (class C) I need to call EnableEvents with
> OnCommEventReceived as an argument. The problem is I do not know how to
> provide a handle to OnCommEventReceived. Since OnCommEventReceived is
> private I guess I need to use reflection, but I cannot make it work.
>
> I have used for instance MethodInfo to invoke private methods before,
> but now I just need some kind of handle to the OnCommEventReceived
> method in class B to provide to the EnableEvents method in class A.

Since you know how to call B.OnCommEventReceived, how about providing
*in class C* a proxy method that has the same signature, and just calls
B.OnCommEventReceived (via reflection). Then just give C.Proxy to
A.EnableEvents.

--
Larry Lard
Replies to group please
Author
28 Feb 2006 2:38 PM
Fredrik Strandberg
Brilliant!

Thank you very much for valuable help!

/Fredrik