Possible solution for dynamic event invokers?

4 views
Skip to first unread message

yesthatmcgurk

unread,
Sep 8, 2008, 2:02:16 PM9/8/08
to Moq Discussions
I was messing around, trying to create a dynamic way to watch an event
to see if it fired. With some fine google fu I was able to create a
dynamic event handler and attach it to any kind of event, be it
generic or not. Here's a link to a solution containing the class and
the barest of tests:

http://cid-f8be9de57b85cc35.skydrive.live.com/self.aspx/Public/DynamicEventWatcher.zip

I was thinking... maybe this could be used to create a mocked event
invoker for any type of event, rather than just generic events?

I haven't spent much time looking at your mocked event invokers, so I
don't know how complex the process is. I suppose if it is trivial you
can snag out the IL and repro the calls when generating the dynamic
event handler. I might look closer at it, tho I've wasted enough time
on it today, probably.

Daniel Cazzulino

unread,
Sep 8, 2008, 2:30:28 PM9/8/08
to moq...@googlegroups.com
creating the generic watcher is the easy part.
thing is, for mocks, you typically want to RAISE the event, which is nowhere as straightforward (need to have a way to do it from the mock instance)

yesthatmcgurk

unread,
Sep 8, 2008, 3:04:16 PM9/8/08
to Moq Discussions
Reflecting on MoQ, I'm not exactly sure how the MockedEvent class can
be used as the event handler itself; that seems bizarre to me.
However, once you get past that, it seems pretty straight forward how
the invocation list is run (side note: you guys do 'foreach' it. If
I could only repro that exception...).

It seems like the easy part is raising the event, the hard part is
getting the compiler not to choke when you add the MockedEven as an
event handler if the MockedEvent doesn't match the expected
signature. I don't understand how the compiler doesn't choke on it
already, so I can't figure out a way to mock any type of event.

On Sep 8, 2:30 pm, "Daniel Cazzulino" <dan...@cazzulino.com> wrote:
> creating the generic watcher is the easy part.
> thing is, for mocks, you typically want to RAISE the event, which is nowhere
> as straightforward (need to have a way to do it from the mock instance)
>
> On Mon, Sep 8, 2008 at 3:02 PM, yesthatmcgurk <yesthatmcg...@gmail.com>wrote:
>
>
>
>
>
> > I was messing around, trying to create a dynamic way to watch an event
> > to see if it fired.  With some fine google fu I was able to create a
> > dynamic event handler and attach it to any kind of event, be it
> > generic or not.  Here's a link to a solution containing the class and
> > the barest of tests:
>
> >http://cid-f8be9de57b85cc35.skydrive.live.com/self.aspx/Public/Dynami...
>
> > I was thinking... maybe this could be used to create a mocked event
> > invoker for any type of event, rather than just generic events?
>
> > I haven't spent much time looking at your mocked event invokers, so I
> > don't know how complex the process is.  I suppose if it is trivial you
> > can snag out the IL and repro the calls when generating the dynamic
> > event handler.  I might look closer at it, tho I've wasted enough time
> > on it today, probably.- Hide quoted text -
>
> - Show quoted text -

Daniel Cazzulino

unread,
Sep 8, 2008, 3:23:44 PM9/8/08
to moq...@googlegroups.com
 here's how it works today:

MockedEvent has an implicit cast to the delegate type, that's how you can += it to an event of that delegate type (EventHandler or EventHandler<T>). That's the easiest syntax I found for events hooking (that's not the ugly "+= null" and IgnoreArguments of Rhino).

In Moq, doing += mockedEvent is actually performing the association between the object and the event it will raise/respond to.

The "magic" happens in the Interceptor.cs class, where I'm looking at the invocation being intercepted: if it's an "add_EventName" then I know someone just did a +=, in that case, I check if the target for the passed-in delegate is a MockedEvent, and therefore know you're actually associating the event with the class. Once the relationship is stablished, I can raise the event from the object.

So, the problem is that trick of implicit cast to the delegate type to get the += handler syntax.

Alternative expression-style syntaxes were harder or impossible... I'd love it if we could find a way to fix this!!! :(
Reply all
Reply to author
Forward
0 new messages