Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Referencing events in C# using interfaces - A simple approach

0.00/5 (No votes)
6 Sep 2012 1  
Passing event references as interfaces in C# by proxifying them within EventReference instances.

Introduction

The following article shows a simple method to "reference" events using interfaces and generics. For the sake of simplicity, it uses Actions and Functions.

Background 

The only closely related article I could find is the following:

Source Code Overview

A non-generic event reference description:

public interface IEventReference { }

can be used to constrain the more concrete, but nevertheless generic event description interfaces.

public interface IActionEventReference<I> where I : IEventReference
{
    event Action Event;
}
public interface IActionEventReference<I, P1> where I : IEventReference
{
    event Action<P1> Event;
}

public interface IFuncEventReference<I, R> where I : IEventReference
{
    event Func<R> Event;
}
public interface IFuncEventReference<I, P1, R> where I : IEventReference
{
    event Func<P1, R> Event;
}

Using the Code  

Consider having a class which exposes some events, for example:

public class ClassExposingEvents
{
  public event Func<bool> BeforeCancel;
}

Adding the possibility to "reference" the BeforeCancel-event can be done in three steps:

First, an interface for the concrete event reference can be created: 

public interface IBeforeCancelEventReference : IEventReference { event Func<bool> BeforeCancel; }

and added to the class which provides the event:

pulic class ClassExposingEvents : IBeforeCancelEventReference
{
  public event Func<bool> BeforeCancel;
}

Second, a proxy class for the event can be defined (I usually do it as a subclass of the event container but this is not necessary):

public class BeforeCancelEventRef : 
         IFuncEventReference<IBeforeCancelEventReference, bool>
{
  IBeforeCancelEventReference e;
  public BeforeCancelEventRef(IBeforeCancelEventReference eventContainer) { e = eventContainer; }
  public event Func<bool> Event
  {
    add { e.BeforeCancel += value; }
    remove { e.BeforeCancel -= value; }
  }
}

Finally, all which need a reference to the BeforeCancel-event just need an instance of the class BeforeCancelEventRef (usually given back as an interface and created by a factory or something else which is appropriate).

To use the reference, we then can just add and remove targets from the Event-property of the reference class:

IFuncEventReference<IBeforeCancelEventReference, bool> bce = ...;
bce.Event += ...;

Points of Interest  

Exceptions and performance are not covered by the article.  

History 

  • 2012/09/06: First version submitted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here