[英]EventAggregator Helper Generic method
Why this gives a compiler error? 为什么这会导致编译器错误?
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;
}
}
The error is: 错误是:
Severity Code Description Project File Line Suppression State Error CS0310 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TEventType' in the generic type or method 'IEventAggregator.GetEvent()' EntitySetGridTWPF D:\\DEVELOPER.NET\\Comercial\\EntityBookCommon\\EntitySetGridTWPF\\EventAggregation\\EventAggregationHelper.cs 9 Active 严重性代码说明项目文件行抑制状态错误CS0310“ T”必须是具有公共无参数构造函数的非抽象类型,以便将其用作通用类型或方法“ IEventAggregator.GetEvent()”中的参数“ TEventType”,EntitySetGridTWPF D :\\ DEVELOPER.NET \\ Comercial \\ EntityBookCommon \\ EntitySetGridTWPF \\ EventAggregation \\ EventAggregationHelper.cs 9有效
on the line: 在线上:
T evt = eventAggregator.GetEvent<T>();
I've used this approach for calling other generic methods before and has worked. 我曾经使用过这种方法来调用其他通用方法,并且已经奏效。 What has GetEvent so special? GetEvent有什么特别之处?
Thanks in advance. 提前致谢。
IEventAggregator.GetEvent
has a new()
constraint, which means that your subscription also needs to add the new()
constraint and that also needs to be fulfilled by your implementation class T
, which must have a public parameterless (default) constructor (and must not be abstract.) 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.