■ IDisposable 헬퍼 사용하기

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

using System;

 

/// <summary>

/// IDisposable 헬퍼 사용하기

/// </summary>

public static class DisposableHelper

{

    #region 사용하기 - Using<TDisposable, TResult>(factoryFunction, function)

 

    /// <summary>

    /// 사용하기

    /// </summary>

    /// <typeparam name="TDisposable">리소스 해제 가능 타입</typeparam>

    /// <typeparam name="TResult">결과 타입</typeparam>

    /// <param name="factoryFunction">팩토리 함수</param>

    /// <param name="function">함수</param>

    /// <returns>결과 타입</returns>

    public static TResult Using<TDisposable, TResult>

    (

        Func<TDisposable>          factoryFunction,

        Func<TDisposable, TResult> function

    ) where TDisposable : IDisposable

    {

        using(TDisposable disposable = factoryFunction())

        {

            return function(disposable);

        }

    }

 

    #endregion

}

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요