小编tur*_*qel的帖子

如何委派正确的Action <Interface>?

我是C#的初学者,找不到任何答案:

我试图用一些接口参数委托动作,但推动使用扩展此接口(或类)的对象的函数

// some class extending interface
public class IntEvent : IFace{}
public class MoveEvent : IFace{}

// function i like to use to verify Action
static void setAction(Action<IFace> evt) {
    // evt ...
}
// function to delegate as Action
static void evtCheck(IntEvent evt) {
    // some func
}
static void evtMove(MoveEvent evt) {
    // some func
}
// class {
//  call method inside class and delegate this function :
setAction(evtCheck);
setAction(evtMove);
Run Code Online (Sandbox Code Playgroud)

即使扩展了界面,我也收到" evtCheck(IntEvent)无法转换为Action<IFace>" 的错误. …

.net c# delegates compiler-errors interface

6
推荐指数
2
解决办法
1337
查看次数

标签 统计

.net ×1

c# ×1

compiler-errors ×1

delegates ×1

interface ×1