leozilla
leozilla

Reputation: 1346

C# same property with different type on different interface

I have two interfaces (one for subscribers and one for publishers):

First one should be visible for clients that are allowed to raise events

   public interface IClientLogicEvents
   {
      IRaiseEvent<CallStateChangedEventArgs> CallStateChanged { get; }
   }

The second one should be visible for clients that subscribe and handle those events

   public interface IHandleOnlyClientLogicEvents : IClientLogicEvents
   {
      ISubscribeEvent<CallStateChangedEventArgs> CallStateChanged { get; }
   }

The Event interface look something like this

   public interface ISubscribeEvent<out TEventArgs> where TEventArgs : EventArgs
   {
      void Subscribe(Action<object, TEventArgs> handler);
      void Unsubscribe(Action<object, TEventArgs> handler);
   }

    public interface IRaiseEvent<TEventArgs> : ISubscribeEvent<TEventArgs> where TEventArgs : EventArgs
   {
      void Raise(object sender, TEventArgs args);
   }

Now i want to have a class that implements both of these interfaces (IClientLogicEvents and IHandleOnlyClientLogicEvents).

Like this:

public sealed class ClientLogicEvents : IClientLogicEvents, IHandleOnlyClientLogicEvents

or:

 public sealed class ClientLogicEvents : IHandleOnlyClientLogicEvents

The problem is now of course is that i need to implement the property twice (for each interface) which requires an extra field to store it.

    public sealed class ClientLogicEvents : IClientLogicEvents, IHandleOnlyClientLogicEvents, IDisposable
   {
      /// <summary>
      /// The internal call state changed event.
      /// </summary>
      private readonly CustomEvent<CallStateChangedEventArgs> _callStateChangedEvent;

      public ClientLogicEvents()
      {
         _callStateChangedEvent = new CustomEvent<CallStateChangedEventArgs>();
      }

      /// <summary>
      /// Gets the invokeable call state changed event.
      /// </summary>
      IRaiseEvent<CallStateChangedEventArgs> IClientLogicEvents.CallStateChanged { get { return _callStateChangedEvent; } }

      /// <summary>
      /// Gets the subscribe only call state changed event.
      /// </summary>
      ISubscribeEvent<CallStateChangedEventArgs> IHandleOnlyClientLogicEvents.CallStateChanged { get { return _callStateChangedEvent; } }
   }

But i would like to save up this amount of code for the property implementation (because i have like 200 events). Is this possible somehow. That i have just something like

    public sealed class ClientLogicEvents : IClientLogicEvents, IHandleOnlyClientLogicEvents, IDisposable
   {
      /// <summary>
      /// Gets the invokeable call state changed event.
      /// </summary>
      public IRaiseEvent<CallStateChangedEventArgs> CallStateChanged { get; private set; }
   }

?

Upvotes: 0

Views: 160

Answers (1)

Jakub Konecki
Jakub Konecki

Reputation: 46008

You cannot have a single property that will satisfy constraints from both interfaces as a single property cannot have two types (IRaiseEvent<CallStateChangedEventArgs> and ISubscribeEvent<CallStateChangedEventArgs>) at the same time.

Your example at the bottom of the question doesn't implement IHandleOnlyClientLogicEvents interface.

Upvotes: 1

Related Questions