Args In Interface General Event

I am trying to store an object in the generic EventArgs class, but since EventHandler has an interface, I find it difficult to accomplish this. Any way to get something like this works?

My EventArgs class:

public class PositionChangedEventArgs<T> { public PositionChangedEventArgs(byte position, T deviceArgs) { Position = position; DeviceArgs = deviceArgs; } public byte Position { get; private set; } public T DeviceArgs { get; private set; } } 

Interface Used:

 public interface IMoveable { event EventHandler<PositionChangedEventArgs<T>> PositionChanged; } 

Class usage example:

 public class SomeDevice : IMoveable { public event EventHandler<PositionChangedEventArgs<DeviceSpecificEventMessageArgs>> PositionChanged; //Compiler doesn't like this } 
+4
source share
2 answers

You need to change the interface definition to the following:

 public interface IMoveable<T> { event EventHandler<PositionChangedEventArgs<T>> PositionChanged; } 

You can use it by passing the type to the interface like this:

 public class SomeDevice : IMoveable<DeviceSpecificEventMessageArgs> { public event EventHandler<PositionChangedEventArgs<DeviceSpecificEventMessageArgs>> PositionChanged; } 
+9
source

This is because you must declare the interface as generic:

 public interface IMoveable<T> { event EventHandler<PositionChangedEventArgs<T>> PositionChanged; } 
0
source

Source: https://habr.com/ru/post/1340819/


All Articles