■ 함수형 확장을 사용하는 방법을 보여준다.
▶ FunctionalExtension.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Threading.Tasks; namespace TestProject { /// <summary> /// 함수형 확장 /// </summary> public static class FunctionalExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 함수 실행하기 - ExecuteFunction<TSource, TResult>(sourceTask, function) /// <summary> /// 함수 실행하기 /// </summary> /// <typeparam name="TSource">소스 타입</typeparam> /// <typeparam name="TResult">결과 타입</typeparam> /// <param name="sourceTask">소스 태스크</param> /// <param name="function">함수</param> /// <returns>태스크</returns> public static async Task<TResult> ExecuteFunction<TSource, TResult> ( this Task<TSource> sourceTask, Func<TSource, Task<TResult>> function ) => await function(await sourceTask); #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using System; using System.Threading.Tasks; namespace TestProject { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region A 곱하기 (비동기) - MultiplyAAsync(a) /// <summary> /// A 곱하기 (비동기) /// </summary> /// <param name="a">A</param> /// <returns>곱셈 결과</returns> private async static Task<int> MultiplyAAsync(int a) => await Task.FromResult(a * 1); #endregion #region B 곱하기 (비동기) - MultiplyBAsync(b) /// <summary> /// B 곱하기 (비동기) /// </summary> /// <param name="b">B</param> /// <returns>곱셈 결과</returns> private async static Task<int> MultiplyBAsync(int b) => await Task.FromResult(b * 2); #endregion #region C 곱하기 (비동기) - MultiplyCAsync(c) /// <summary> /// C 곱하기 (비동기) /// </summary> /// <param name="c">C</param> /// <returns>곱셈 결과</returns> private async static Task<int> MultiplyCAsync(int c) => await Task.FromResult(c * 3); #endregion #region 곱하기 - Multiply() /// <summary> /// 곱하기 /// </summary> public async static void Multiply() { int result = await MultiplyAAsync(10) .ExecuteFunction(MultiplyBAsync) .ExecuteFunction(MultiplyCAsync); Console.WriteLine("결과 : {0}", result); } #endregion #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Multiply(); } #endregion } } |