C#扩展方法(Extension Method)

在 C# 中,扩展方法(Extension Method)的语法允许你通过 this 关键字将第一个参数隐式传递,因此在调用时 ​不需要显式传递第一个参数。

这里有个扩展方法

csharp 复制代码
public static Task<T> InvokeAsync<T>(this Control control, Func<T> func)
{
    var tcs = new TaskCompletionSource<T>();
    control.BeginInvoke(new Action(() =>
    {
        try
        {
            tcs.SetResult(func());
        }
        catch (Exception ex)
        {
            tcs.SetException(ex);
        }
    }));
    return tcs.Task;
}

扩展方法的特点:

复制代码
**​隐式 control 参数:**
    this Control control 表示这是一个扩展方法,作用在 Control 类型上。
    当你调用 _uiControl.InvokeAsync(showDialogFunc) 时,_uiControl 会自动作为 control 参数传递,而 showDialogFunc 是第二个参数 func。
**​泛型参数 <T>:**
    InvokeAsync<T> 的泛型参数 <T> 会根据 func 的返回值类型自动推断。例如,如果 showDialogFunc 返回 string,则 T 会被推断为 string。

调用时的参数传递:

csharp 复制代码
return await _uiControl.InvokeAsync(showDialogFunc);
复制代码
_uiControl 是扩展方法的第一个参数 control(通过 this Control control 隐式传递)。
showDialogFunc 是第二个参数 func。

为什么不需要显式传递两个参数?

扩展方法的语法简化了调用方式:

原本需要写为 InvokeAsync(_uiControl, showDialogFunc)。

但因为 this Control control 的存在,你可以直接写成 _uiControl.InvokeAsync(showDialogFunc),编译器会自动将 _uiControl 作为第一个参数。

完整流程解析:

线程切换:

如果当前线程不是 UI 线程,control.BeginInvoke 会将 func 委托的调用调度到 UI 线程。

如果当前已经是 UI 线程,BeginInvoke 会直接执行(同步或异步取决于具体实现)。
​异步包装:

TaskCompletionSource 用于将 BeginInvoke 的异步操作包装成一个 Task。
当 func 在 UI 线程执行完成后:

如果成功,调用 tcs.SetResult(func()) 设置结果。

如果抛出异常,调用 tcs.SetException(ex) 传递异常。
​ 返回 Task:

最终返回的 tcs.Task 可以让调用者通过 await 等待结果。

总结:

​**扩展方法的语法:通过 this 关键字隐式传递第一个参数(control),调用时不需要显式传递。
泛型推断:**根据 func 的返回值类型自动推断泛型参数 。
​线程安全: BeginInvoke 确保 func 在 UI 线程执行,并通过 Task 提供异步支持。

相关推荐
望获linux7 分钟前
【Linux基础知识系列】第六十四篇 - 了解Linux的硬件架构
linux·运维·服务器·开发语言·数据库·操作系统·嵌入式软件
1candobetter1 小时前
JAVA后端开发——用 Spring Boot 实现定时任务
java·开发语言·spring boot
啊阿狸不会拉杆2 小时前
《Java 程序设计》第 8 章 - Java 常用核心类详解
java·开发语言·python·算法·intellij-idea
小白学大数据2 小时前
Python + Requests库爬取动态Ajax分页数据
开发语言·python·ajax·okhttp
2501_920047032 小时前
python-内存管理
开发语言·jvm·python
小乖兽技术2 小时前
C#与C++交互开发系列(二十六):构建跨语言共享缓存,实现键值对读写与数据同步(实践方案)
c++·c#·交互
芜青2 小时前
JavaScript手录09-内置对象【String对象】
开发语言·javascript·ecmascript
钮钴禄·爱因斯晨2 小时前
Java 集合进阶:从 Collection 接口到迭代器的实战指南
java·开发语言
mrbone113 小时前
C++-关于协程的一些思考
开发语言·数据库·c++·c++20·协程·异步·coroutines
zgc12453673 小时前
Linux学习--C语言(指针3)
c语言·开发语言·学习