为什么这会导致编译器错误?
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;
}
}错误是:
严重性代码描述项目文件行抑制状态错误D:\DEVELOPER.NET\Comercial\EntityBookCommon\EntitySetGridTWPF\EventAggregation\EventAggregationHelper.cs 'T‘必须是具有公共无参数构造函数的非抽象类型,才能将其用作泛型类型或方法'IEventAggregator.GetEvent()’EntitySetGridTWPF EntitySetGridTWPF 9活动中的参数'TEventType‘。
在线路上:
T evt = eventAggregator.GetEvent<T>();我以前使用过这种方法来调用其他泛型方法,并且起了作用。GetEvent有什么特别之处?
提前谢谢。
发布于 2016-10-10 10:57:09
IEventAggregator.GetEvent有一个new()约束,这意味着您的订阅也需要添加new()约束,这也需要由实现类T来实现,它必须有一个公共的无参数(默认)构造函数(而且不能是抽象的)。
public static SubscriptionToken SubscribeToEvent<T>
(IEventAggregator eventAggregator) where T : EventBase, new() {https://stackoverflow.com/questions/39956619
复制相似问题