Click here to Skip to main content
15,886,199 members
Articles / Containers
Tip/Trick

Referencing events in C# using interfaces - A simple approach

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Sep 2012CPOL1 min read 20.5K   7   3
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:

C#
public interface IEventReference { }

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

C#
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:

C#
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: 

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

and added to the class which provides the event:

C#
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):

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Switzerland Switzerland
coding for fun and money

Comments and Discussions

 
QuestionEvents Interface for Mdi and dinamically loaded childs Pin
AngelBlueSky10-Dec-13 3:49
AngelBlueSky10-Dec-13 3:49 
AnswerRe: Events Interface for Mdi and dinamically loaded childs Pin
saephoed11-Dec-13 6:46
saephoed11-Dec-13 6:46 
GeneralRe: Events Interface for Mdi and dinamically loaded childs Pin
AngelBlueSky16-Apr-15 21:57
AngelBlueSky16-Apr-15 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.