c# - Inject an event as a dependency -


i need class handle system.windows.forms.application.idle - however, i'm wanting remove specific dependency can unit test it. ideally, want pass in in constructor - like:

var myobj = new myclass(system.windows.forms.application.idle); 

currently, it's complaining can use event += , -= operators. there way this?

you can abstract event behind interface:

public interface iidlingsource {     event eventhandler idle; }  public sealed class applicationidlingsource : iidlingsource {     public event eventhandler idle     {         add { system.windows.forms.application.idle += value; }         remove { system.windows.forms.application.idle -= value; }     } }  public class myclass {     public myclass(iidlingsource idlingsource)     {         idlingsource.idle += onidle;     }      private void onidle(object sender, eventargs e)     {         ...     } }  // usage  new myclass(new applicationidlingsource()); 

Comments