繁体   English   中英

EventAggregator Helper通用方法

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM