0
我有兩個接口(一個用於用戶和一個出版商):C#相同的屬性有不同類型的不同接口上
第一個應該爲被允許引發事件
public interface IClientLogicEvents
{
IRaiseEvent<CallStateChangedEventArgs> CallStateChanged { get; }
}
客戶可見第二個應該爲訂閱用戶可見和處理這些事件
public interface IHandleOnlyClientLogicEvents : IClientLogicEvents
{
ISubscribeEvent<CallStateChangedEventArgs> CallStateChanged { get; }
}
Event接口是這個樣子
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; }
}
?
在C#中,當你擴展它時,你不能改變某些東西的類型,所以你的解決方案必須有些複雜。 –
假設你找到了一種方法,你認爲200個事件可能會使你難以找到你想要附加的事件嗎? – weston
你確定你不需要一些消息總線的實現,比如[Prism的EventAggregator](http://www.codeproject.com/Articles/355473/Prism-EventAggregator-Sample)?因爲擁有所有這些事件的單點訪問看起來完全一樣。 – galenus