开发者

Using MulticastDelegate as parameter while avoiding DynamicInvoke

开发者 https://www.devze.com 2023-02-06 08:15 出处:网络
I have a MulticastDelegate that can reference one of a number of (legacy) delegates that have the same signature. For example:

I have a MulticastDelegate that can reference one of a number of (legacy) delegates that have the same signature. For example:

public delegate vo开发者_StackOverflow中文版id ObjectCreated(object sender, EventArgs args);
public delegate void ObjectDeleted(object sender, EventArgs args);
//...

Those delegates are then used to define events:

public event ObjectCreated ObjectWasCreated;
public event ObjectDeleted ObjectWasDeleted;

I then have a method which takes in a MulticastDelegate that I use to do some common checking:

void DispatchEvent(MulticastDelegate handler, object sender, EventArgs args)
{
    if (handler != null)
    {
        // ...
        handler.DynamicInvoke(sender, args);
    }
}

Which is called from within other methods of the class wherein the events were defined:

DispatchEvent(ObjectWasCreated, sender, args);
DispatchEvent(ObjectWasDeleted, sender, args);

Is there a more concise way to do this that avoids DynamicInvoke?


Here's my reflection-free solution. It basically implements a multicast delegate as a list. Less code? No. Better performance? I don't know. Cleaner? Meh.

public delegate void ObjectCreated(object sender, EventArgs args);
public delegate void ObjectDeleted(object sender, EventArgs args);

public event ObjectCreated ObjectWasCreated
{
    add
    {
        m_ObjectCreatedSubscribers.Add(value.Invoke);
    }
    remove
    {
        m_ObjectCreatedSubscribers.RemoveAll(e => e.Target.Equals(value));
    }
}
public event ObjectDeleted ObjectWasDeleted
{
    add
    {
        m_ObjectDeletedSubscribers.Add(value.Invoke);
    }
    remove
    {
        m_ObjectDeletedSubscribers.RemoveAll(e => e.Target.Equals(value));
    }
}

private List<Action<object, EventArgs>> m_ObjectCreatedSubscribers = new List<Action<object, EventArgs>>();
private List<Action<object, EventArgs>> m_ObjectDeletedSubscribers = new List<Action<object, EventArgs>>();

void DispatchEvent(List<Action<object, EventArgs>> subscribers, object sender, EventArgs args)
{
    foreach (var subscriber in subscribers)
        subscriber(sender, args);
}


One simple alternative is to use built in types like Action<,> or EventHandler instead of custom delegates, so that you get strong types.

public static event Action<object, EventArgs> ObjectWasCreated;
public static event Action<object, EventArgs> ObjectWasDeleted;  

void DispatchEvent(Action<object, EventArgs> handler, object sender, EventArgs args) 
{
    if (handler != null)
    {
        // ...
        handler(sender, args);
    }
}

or

public static event EventHandler ObjectWasCreated;
public static event EventHandler ObjectWasDeleted;  

void DispatchEvent(EventHandler handler, object sender, EventArgs args) 
{
    if (handler != null)
    {
        // ...
        handler(sender, args);
    }
}

Now your method call will be straightforward.

DispatchEvent(ObjectWasCreated, sender, args);
DispatchEvent(ObjectWasDeleted, sender, args);

But that's mostly not a good solution.

You could use dynamic, still much better than DynamicInvoke:

void DispatchEvent(MulticastDelegate handler, object sender, EventArgs args) 
{
    if (handler != null)
    {
        // ...
        ((dynamic)handler)(sender, args);
    }
}

Or may be generics:

void DispatchEvent<T>(T handler, object sender, EventArgs args) 
{
    if (handler != null)
    {
        // ...
        ((dynamic)handler)(sender, args);
    }
}

I did a small performance comparison and found dynamic to be too good actually:

For a million tries

MulticastDelegate + dynamic (first example) => 40 ms

generic + dynamic (second example) => 90 ms

MulticastDelegate + DynamicInvoke (given in question originally) => 940 ms


You could do something like:

void DispatchEvent(MulticastDelegate handler, object sender, EventArgs args)
{
    EventHandler eventHandler = 
        (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), handler.GetType().GetMethod("Invoke"));

    eventHandler(sender, args);
}

I'm not sure if this'll be faster than using DynamicInvoke, though.

You'll have to use reflection somewhere. If each delegate could be guarenteed to only have one subscriber, then you could use the Delegate.Method property directly when creating the EventHandler, but as they're events, they're likely to have more than one subscriber...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号