[英]EventAggregator Helper Generic method
為什么這會導致編譯器錯誤?
public class EventAggregationHelper {
public static SubscriptionToken SubscribeToEvent<T>( IEventAggregator eventAggregator ) where T : EventBase {
T evt = eventAggregator.GetEvent<T>();
//T evt = eventAggregator.GetEvent<T>();
return null;
}
}
錯誤是:
嚴重性代碼說明項目文件行抑制狀態錯誤CS0310“ T”必須是具有公共無參數構造函數的非抽象類型,以便將其用作通用類型或方法“ IEventAggregator.GetEvent()”中的參數“ TEventType”,EntitySetGridTWPF D :\\ DEVELOPER.NET \\ Comercial \\ EntityBookCommon \\ EntitySetGridTWPF \\ EventAggregation \\ EventAggregationHelper.cs 9有效
在線上:
T evt = eventAggregator.GetEvent<T>();
我曾經使用過這種方法來調用其他通用方法,並且已經奏效。 GetEvent有什么特別之處?
提前致謝。
IEventAggregator.GetEvent
具有new()
約束,這意味着您的訂閱還需要添加new()
約束,並且還必須由實現類T
來實現,實現類T
必須具有公共的無參數(默認)構造函數(並且必須不是抽象的。)
public static SubscriptionToken SubscribeToEvent<T>
(IEventAggregator eventAggregator) where T : EventBase, new() {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.