我有两个接口(一个用于订阅者,一个用于发布者):
第一个应该对允许引发事件的客户端可见
public interface IClientLogicEvents
{
IRaiseEvent<CallStateChangedEventArgs> CallStateChanged { get; }
}
第二个应该对订阅和处理这些事件的客户端可见
public interface IHandleOnlyClientLogicEvents : IClientLogicEvents
{
ISubscribeEvent<CallStateChangedEventArgs> CallStateChanged { get; }
}
事件界面看起来像这样
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);
}
现在我想要一个实现这两个接口(IClientLogicEvents 和 IHandleOnlyClientLogicEvents)的类。
像这样:
public sealed class ClientLogicEvents : IClientLogicEvents, IHandleOnlyClientLogicEvents
或者:
public sealed class ClientLogicEvents : IHandleOnlyClientLogicEvents
现在的问题当然是我需要实现该属性两次(对于每个接口),这需要一个额外的字段来存储它。
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; } }
}
但我想为属性实现保存这么多代码(因为我有 200 个事件)。这是否可能以某种方式。我有类似的东西
public sealed class ClientLogicEvents : IClientLogicEvents, IHandleOnlyClientLogicEvents, IDisposable
{
/// <summary>
/// Gets the invokeable call state changed event.
/// </summary>
public IRaiseEvent<CallStateChangedEventArgs> CallStateChanged { get; private set; }
}
?