■ 객체 복사하기 예제

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

using System.Windows.Forms;

 

TextBox tbSource = new TextBox();

 

tbSource.Name = "Source";

 

TextBox tbTarget = CopyObject(tbSource) as TextBox;

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

 

■ 객체 복사하기

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

using System.Reflection;

 

#region 객체 복사하기 - CopyObject(sourceObject)

 

/// <summary>

/// 객체 복사하기

/// </summary>

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

/// <returns>복사 객체</returns>

public object CopyObject(object sourceObject)

{

    Type sourceType = sourceObject.GetType();

 

    PropertyInfo[] sourcePropertyInfoArray = sourceType.GetProperties();

 

    object targetObject = sourceType.InvokeMember("", BindingFlags.CreateInstance, null, sourceObject, null);

 

    foreach(PropertyInfo propertyInfo in sourcePropertyInfoArray)

    {

        if(propertyInfo.CanWrite)

        {

            propertyInfo.SetValue(targetObject, propertyInfo.GetValue(sourceObject, null), null);

        }

    }

 

    return targetObject;

}

 

#endregion

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

Posted by 사용자 icodebroker

댓글을 달아 주세요