■ 객체를 복사하는 방법을 보여준다.
▶ 객체 복사하기 예제 (C#)
1 2 3 4 5 6 7 8 9 |
using System.Windows.Forms; TextBox tbSource = new TextBox(); tbSource.Name = "Source"; TextBox tbTarget = CopyObject(tbSource) as TextBox; |
▶ 객체 복사하기 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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 |