开发者

Howto tell if generated event is sent by it's own class

Is there a way to tell if an event is raised in it's own caller. Essentially I have 1.N classes, all essentially the same, each run by a thread. I have an enum which differentiates the basic types. I have a delegate in the top level class, ExecutionState, which creates all the sub classes, (stores them in a collection). I have the following in each class. ExecutionState is the top level creator class which contains the delegate and the associated event as

    public void Subscribe(ExecutionState ExecState)
    {
      开发者_Go百科  if (this.ExpressionInstance.IsMultiLine)
        {
            ExecState.MultiMatchEvent += new ExecutionState.MultiMatch(Signal);
        }
    }

    private void Signal(int ProcessorPosition)
    { 
    }

    private void SendEvent(ExecutionState.MultiMatch Match)
    {
        if ((Status & RuleState.HasEnd) == RuleState.HasEnd)
        {
            Match(CurrentProcessorNumber);
        }
    }

ExecutionState is the top level creator class which contains the delegate and the associated event as:

public delegate void MultiMatch(int ProcessorPosition);     
public event MultiMatch MultiMatchEvent;

When I call SendEvent, I assume an event will arrive at Signal, at its own class. Of course, Subscribe only needs to be called when the specific conditions I've setup in ExecutionState, exists, i.e. in 1 class, which won't be the event sender class (by design).

I know it's probably been answered many times in the past. Is it a case of doing mechanically, perhaps setting a flag in the event parameters to define it as coming from it's own class, or is their a more elegant way of doing it.

Thanks. Bob.


If you look at the signature of the vanilla EventHandler class, the first parameter is object sender. This is a typical pattern in the .NET framework, and it allows you to compare for equality with the object which raised the event:

public EventHandler(object sender, EventArgs e)
{
   if(this.Equals(sender))
   {
       //...
   }
}

Following this idiom would be well understood by developers using your API, and as such makes sense to follow, IMO.


If you look at the events in say, ASP.NET, you'll find pretty much all of the handlers have a signature like this:

public delegate EventHandler(object _sender, EventArgs _args);

Where, of course, EventArgs is some EventArgs-derived class. _sender always refers to whatever object is raising the event.

You could implement something similar, either in the delegate-signature as outlined above, or in a custom EventArgs-esque class, the abstract base of which might look something like this:

public abstract class MyEventArgs
{
    private object m_Sender = null;

    public MyEventArgs(object _sender)
    {
        m_Sender = _sender;
    } // eo ctor

    public object Sender { get { return m_Sender; } }
} // eo class MyEventArgs

Ensuring of course that all your event-objects derive from this:

public class SomeEventArgs : MyEventArgs { /* ... */ }

You would then raise your events thusly:

if(OnMyEvent != null)
    OnMyEvent(new SomeEventArgs(this));

Recipients can then do a comparison to see if it was, indeed, them who sent it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜