Click here to Skip to main content
Click here to Skip to main content

Referencing events in C# using interfaces - A simple approach

, 6 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zaphoed
Software Developer
Switzerland Switzerland
experienced in asm/c/c++/c#. Less experienced in Python, Lua, Shell and stuff like ASP, PHP, HTML ...
coding for windows, macOS, iOS and linux
just4fun: ogre3d

Comments and Discussions

 
QuestionEvents Interface for Mdi and dinamically loaded childs Pinmemberangelo1110-Dec-13 4:49 
AnswerRe: Events Interface for Mdi and dinamically loaded childs Pinmemberzaphoed11-Dec-13 7:46 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by zaphoed
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid