첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ Student.cs

namespace TestProject
{
    /// <summary>
    /// 학생
    /// </summary>
    public class Student
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region ID - ID

        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }

        #endregion
        #region 성명 - Name

        /// <summary>
        /// 성명
        /// </summary>
        public string Name { get; set; }

        #endregion
    }
}

 

300x250

 

▶ Program.cs

using System.Linq.Expressions;

namespace TestProject;

/// <summary>
/// 프로그램
/// </summary>
class Program
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

    #region 속성명 구하기 - GetPropertyName<T>(expression)

    /// <summary>
    /// 속성명 구하기
    /// </summary>
    /// <typeparam name="T">속성 타입</typeparam>
    /// <param name="expression">수식</param>
    /// <returns>속성명</returns>
    private static string GetPropertyName<T>(Expression<Func<T>> expression)
    {
        MemberExpression memberExpression = expression.Body as MemberExpression;

        if(memberExpression == null)
        {
            throw new ArgumentException("You must pass a lambda of the form : '() => Class.Property' or '() => object.Property'");
        }

        return memberExpression.Member.Name;
    }

    #endregion
    #region 프로그램 시작하기 - Main()

    /// <summary>
    /// 프로그램 시작하기
    /// </summary>
    private static void Main()
    {
        Student student = new Student { ID = 1, Name = "홍길동" };

        string name = GetPropertyName(() => student.Name);

        Console.WriteLine(name);
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요