加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

对于Action <>和/或Func <>,c#中的typede

发布时间:2020-12-15 08:09:10 所属栏目:百科 来源:网络整理
导读:谷歌搜索后看起来并不乐观,但我想知道在使用Action T时是否存在某种混叠或类型定义方式.或者Func in T,out TResult在C#? 我已经看过Equivalent of typedef in c#了,它说在一个编译范围内你可以在某些情况下使用using构造,但就我所知,这似乎并不适用于Action
谷歌搜索后看起来并不乐观,但我想知道在使用Action< T>时是否存在某种混叠或类型定义方式.或者Func< in T,out TResult>在C#?

我已经看过Equivalent of typedef in c#了,它说在一个编译范围内你可以在某些情况下使用using构造,但就我所知,这似乎并不适用于Action和Func.

我想要这样做的原因是我想要一个动作用作多个函数的参数,如果我在某个时间点决定改变动作,那么很多地方都要改变它们作为参数类型和作为变量类型.

?typedef? MyAction  Action<int,int>;

public static SomeFunc(WindowClass window,int number,MyAction callbackAction) {
   ...
   SomeOtherFunc(callbackAction);
   ...
}

// In another file/class/...
private MyAction theCallback;

public static SomeOtherFunc(MyAction callbackAction) {
    theCallback = callbackAction;
}

是否有一些构造要使用,它可以定义代码段中指示的MyAction?

解决方法

在进行了一些搜索之后,似乎代表们正在拯救(见 Creating delegates manually vs using Action/Func delegates和 A:
Custom delegate types vs Func and Action).请评论为什么这不是解决方案或可能的陷阱.

有了代理,我可以重写给定代码示例的第一行:

public delegate void MyAction(int aNumber,int anotherNumber);
// Keep the rest of the code example

// To call one can still use anonymous actions/func/...
SomeFunc(myWindow,109,(int a,int b) => Console.Writeline);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读