C# 以函数Action/Func/Task作为方法参数

C#

浏览数:952

2019-6-14

以Action、Func、Task作为方法参数,mark一下

以Action为参数

 1     public void TestAction()
 2     {
 3         //Action参数
 4         ExecuteFunction(() =>
 5         {
 6 
 7         });
 8         ExecuteFunction(ActionAAA("para"));
 9         ExecuteFunction(AAA);
10 
11         //Action<T>参数,T可以由类的泛型参数输入
12         ExecuteFunction<string>((string praa) =>
13         {
14 
15         });
16         //Action<T>参数,T主动输入
17         ExecuteFunction<string>((string praa) =>
18         {
19 
20         }, "aaa");
21         ExecuteFunction<string>(AAA, "para");
22     }
23 
24     private void ExecuteFunction(Action action)
25     {
26         action?.Invoke();
27     }
28 
29     private void ExecuteFunction<T>(Action<T> action)
30     {
31         return;
32     }
33     private void ExecuteFunction<T>(Action<T> action, T s)
34     {
35         action?.Invoke(s);
36     }

引用的方法


 1     void AAA()
 2     {
 3     }
 4     void AAA(string para)
 5     {
 6     }
 7     Action ActionAAA(string para)
 8     {
 9         return () =>
10         {
11             Console.WriteLine(para);
12         };
13     }

以Func为参数 

 1     public async void TestFunc()
 2     {
 3         //Func<string>
 4         ExecuteFunction(() =>
 5         {
 6             return string.Empty;
 7         });
 8         //Func<Task>
 9         ExecuteFunction(async () => { });
10         ExecuteFunction(FuncTask);
11         //Func<T, Tout>
12         ExecuteFunction<string, int>(BBB, "para");
13         //Func<T, Task>
14         ExecuteFunction<string>(FuncTask, "para");
15         //Func<T, Task<Tout>>
16         var stringResult = await ExecuteFunction<string, string>(FuncWithResult<string, string>, "para");
17     }
18 
19     private string ExecuteFunction(Func<string> func)
20     {
21         return func.Invoke();
22     }
23 
24     private void ExecuteFunction(Func<Task> func)
25     {
26         func?.Invoke();
27     }
28     private Tout ExecuteFunction<T, Tout>(Func<T, Tout> func, T para)
29     {
30         return func.Invoke(para);
31     }
32 
33     private void ExecuteFunction<T>(Func<T, Task> function, T para)
34     {
35         function?.Invoke(para);
36     }
37     private async Task<Tout> ExecuteFunction<T, Tout>(Func<T, Task<Tout>> function, T para)
38     {
39         return await function?.Invoke(para);
40     }


 1     private int BBB(string obj)
 2     {
 3         return 1;
 4     }
 5     private async Task<Tout> FuncWithResult<T, Tout>(T para)
 6     {
 7         await Task.Delay(2);
 8         return default(Tout);
 9     }
10 
11     async Task FuncTask<T>(T para)
12     {
13 
14     }
15     async Task FuncTask()
16     {
17 
18     }

其中,

    ExecuteFunction(async () =>

    {     }); 输入async () =>{}后,Func<Task> func、Action action都是可以匹配的,优先使用的是Func<Task> func  

以Task为参数

 1     private void TestTask()
 2     {
 3         ExecuteFunction(Task.Run(() =>
 4         {
 5 
 6         }));
 7         ExecuteFunction(Task<string>.Run(async () =>
 8         {
 9             return string.Empty;
10         }));
11     }
12 
13     private void ExecuteFunction(Task task)
14     {
15         task.Start();
16     }
17     private void ExecuteFunction<Tout>(Task<Tout> task)
18     {
19         task.Start();
20     }

 

作者:唐宋元明清2188