■ 동적 객체 사용하기

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

using System;

using System.Reflection;

 

/// <summary>

/// 동적 객체

/// </summary>

public class DynamicObject

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field

    ////////////////////////////////////////////////////////////////////////////////////////// Private

 

    #region Field

 

    /// <summary>

    /// 객체 타입

    /// </summary>

    private Type objectType;

 

    /// <summary>

    /// 소스 객체

    /// </summary>

    private object sourceObject;

 

    /// <summary>

    /// 바인딩 플래그

    /// </summary>

    private BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 객체 타입 - ObjectType

 

    /// <summary>

    /// 객체 타입

    /// </summary>

    public Type ObjectType

    {

        get

        {

            return this.objectType;

        }

    }

 

    #endregion

    #region 소스 객체 - SourceObject

 

    /// <summary>

    /// 소스 객체

    /// </summary>

    public object SourceObject

    {

        get

        {

            return this.sourceObject;

        }

    }

 

    #endregion

    #region 바인딩 플래그 - BindingFlags

 

    /// <summary>

    /// 바인딩 플래그

    /// </summary>

    public BindingFlags BindingFlags

    {

        get

        {

            return this.bindingFlags;

        }

 

        set

        {

            this.bindingFlags = value;

        }

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 생성자 - DynamicObject(sourceObject)

 

    /// <summary>

    /// 생성자

    /// </summary>

    /// <param name="sourceObject">소스 객체</param>

    public DynamicObject(Object sourceObject)

    {

        if(sourceObject == null)

        {

            throw new ArgumentNullException("sourceObject");

        }

 

        this.sourceObject = sourceObject;

        this.objectType   = sourceObject.GetType();

    }

 

    #endregion

    #region 생성자 - DynamicObject(objectType)

 

    /// <summary>

    /// 생성자

    /// </summary>

    /// <param name="objectType">객체 타입</param>

    public DynamicObject(Type objectType)

    {

        if(objectType == null)

        {

            throw new ArgumentNullException("objectType");

        }

 

        this.objectType = objectType;

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 소스 객체 설정하기 - SetSourceObject(parameterTypeArray, parameterValueArray)

 

    /// <summary>

    /// 소스 객체 설정하기

    /// </summary>

    /// <param name="parameterTypeArray">매개 변수 타입 배열</param>

    /// <param name="parameterValueArray">매개 변수 값 배열</param>

    public void SetSourceObject(Type[] parameterTypeArray, object[] parameterValueArray)

    {

        ConstructorInfo constructorInfo = this.objectType.GetConstructor(parameterTypeArray);

 

        if(constructorInfo == null)

        {

            throw new Exception("The constructor matching the specified parameter types is not found.");

        }

 

        this.sourceObject = constructorInfo.Invoke(parameterValueArray);

    }

 

    #endregion

    #region 소스 객체 생성하기 - SetSourceObject()

 

    /// <summary>

    /// 소스 객체 생성하기

    /// </summary>

    public void SetSourceObject()

    {

        SetSourceObject(new Type[0], new object[0]);

    }

 

    #endregion

 

    #region 속성 값 구하기 - GetPropertyValue(propertyName)

 

    /// <summary>

    /// 속성 값 구하기

    /// </summary>

    /// <param name="propertyName">속성명</param>

    /// <returns>속성 값</returns>

    public object GetPropertyValue(string propertyName)

    {

        object propertyValue = this.objectType.InvokeMember(propertyName, BindingFlags.GetProperty | bindingFlags, null,

            this.sourceObject, null);

 

        return propertyValue;

    }

 

    #endregion

    #region 속성 값 설정하기 - SetPropertyValue(propertyName, propertyValue)

 

    /// <summary>

    /// 속성 값 설정하기

    /// </summary>

    /// <param name="propertyName">속성명</param>

    /// <param name="propertyValue">속성 값</param>

    public void SetPropertyValue(string propertyName, object propertyValue)

    {

        this.objectType.InvokeMember(propertyName, BindingFlags.SetProperty | this.bindingFlags, null,

            this.sourceObject, new object[] { propertyValue });

    }

 

    #endregion

 

    #region 필드 값 구하기 - GetFieldValue(fieldName)

 

    /// <summary>

    /// 필드 값 구하기

    /// </summary>

    /// <param name="fieldName">필드명</param>

    /// <returns>필드 값</returns>

    public object GetFieldValue(string fieldName)

    {

        object fieldValue = this.objectType.InvokeMember(fieldName, BindingFlags.GetField | this.bindingFlags, null,

            this.sourceObject, null);

 

        return fieldValue;

    }

 

    #endregion

    #region 필드 값 설정하기 - SetFieldValue(fieldName, fieldValue)

 

    /// <summary>

    /// 필드 값 설정하기

    /// </summary>

    /// <param name="fieldName">필드명</param>

    /// <param name="fieldValue">필드 값</param>

    public void SetFieldValue(string fieldName, object fieldValue)

    {

        this.objectType.InvokeMember(fieldName, BindingFlags.SetField | this.bindingFlags, null, this.sourceObject,

            new object[] { fieldValue });

    }

 

    #endregion

 

    #region 메소드 실행하기 - ExecuteMethod(methodName, parameterValueArray)

 

    /// <summary>

    /// 메소드 실행하기

    /// </summary>

    /// <param name="methodName">메소드명</param>

    /// <param name="parameterValueArray">매개 변수 값 배열</param>

    /// <returns>반환 값</returns>

    public object ExecuteMethod(string methodName, params object[] parameterValueArray)

    {

        object returnValue = this.objectType.InvokeMember(methodName, BindingFlags.InvokeMethod | this.bindingFlags,

            nullthis.sourceObject, parameterValueArray);

 

        return returnValue;

    }

 

    #endregion

    #region 메소드 실행하기 - ExecuteMethod(methodName, parameterTypeArray, parameterValueArray)

 

    /// <summary>

    /// 메소드 실행하기

    /// </summary>

    /// <param name="methodName">메소드명</param>

    /// <param name="parameterTypeArray">매개 변수 타입 배열</param>

    /// <param name="parameterValueArray">매개 변수 값 배열</param>

    /// <returns>반환 값</returns>

    public object ExecuteMethod(string methodName, Type[] parameterTypeArray, object[] parameterValueArray)

    {

        if(parameterTypeArray.Length != parameterValueArray.Length)

        {

            throw new ArgumentException("The type for each parameter values must be specified.");

        }

 

        MethodInfo methodInfo = this.objectType.GetMethod(methodName, parameterTypeArray);

 

        if(methodInfo == null)

        {

            throw new ApplicationException(string.Format("The method {0} is not found.", methodName));

        }

 

        object returnValue = methodInfo.Invoke(this.sourceObject, bindingFlags, null, parameterValueArray, null);

 

        return returnValue;

    }

 

    #endregion

}

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

Posted by 사용자 icodebroker