■ 변환 함수를 사용해 객체 변환하기 예제

------------------------------------------------------------------------------------------------------------------------

using System;

 

Func<string> resultFunction = Convert<string, string>(() => "sun", x => { return () => x.ToUpper(); });

 

Console.WriteLine(resultFunction());

------------------------------------------------------------------------------------------------------------------------

 

■ 변환 함수를 사용해 객체 변환하기

------------------------------------------------------------------------------------------------------------------------

using System;

 

#region 변환하기 - Convert<TSource, TTarget>(sourceFunction, convertFunction)

 

/// <summary>

/// 변환하기

/// </summary>

/// <typeparam name="TSource">소스 타입</typeparam>

/// <typeparam name="TTarget">타겟 타입</typeparam>

/// <param name="sourceFunction">소스 함수</param>

/// <param name="convertFunction">변환 함수</param>

/// <returns>타겟 함수</returns>

public Func<TTarget> Convert<TSource, TTarget>

(

    Func<TSource>                sourceFunction,

    Func<TSource, Func<TTarget>> convertFunction

)

{

    return () =>

    {

        TSource source = sourceFunction();

 

        Func<TTarget> targetFunction = convertFunction(source);

 

        return targetFunction();

    };

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

 

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요