[BLENDER] Object Mode에서 Object 조작하기
■ Object Mode에서 Object를 조작하는 방법을 보여준다. 카테고리 : SOFTWARE/BLENDER 태그 : BLENDER,OBJECT • Object Mode와 Edit Mode가 있다. • Object Mode는
■ Object Mode에서 Object를 조작하는 방법을 보여준다. 카테고리 : SOFTWARE/BLENDER 태그 : BLENDER,OBJECT • Object Mode와 Edit Mode가 있다. • Object Mode는
■ ICloneable 인터페이스에서 객체 DEEP COPY를 사용하는 방법을 보여준다. ▶ School.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using System; namespace TestProject { /// <summary> /// 학교 /// </summary> public class School : ICloneable { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get; set; } #endregion #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 객체 SHALLOW 복사하기 - ShallowCopy() /// <summary> /// 객체 SHALLOW 복사하기 /// </summary> /// <returns>복사 객체</returns> public School ShallowCopy() { return (School)MemberwiseClone(); } #endregion #region 복제하기 - Clone() /// <summary> /// 복제하기 /// </summary> /// <returns>복제 객체</returns> public School Clone() { return DeepCopy(); } #endregion #region 복제하기 - ICloneable.Clone() /// <summary> /// 복제하기 /// </summary> /// <returns>복제 객체</returns> object ICloneable.Clone() { return Clone(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 객체 DEEP 복사하기 - DeepCopy() /// <summary> /// 객체 DEEP 복사하기 /// </summary> /// <returns>복사 객체</returns> protected virtual School DeepCopy() { School school = new School(); school.ID = ID; school.Name = string.Copy(Name); return school; } #endregion } } |
▶ Student.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
using System; namespace TestProject { /// <summary> /// 학생 /// </summary> public class Student : ICloneable { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get; set; } #endregion #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #endregion #region 학교 - School /// <summary> /// 학교 /// </summary> public School School { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 객체 SHALLOW 복사하기 - ShallowCopy() /// <summary> /// 객체 SHALLOW 복사하기 /// </summary> /// <returns>복사 객체</returns> public Student ShallowCopy() { return (Student)MemberwiseClone(); } #endregion #region 복제하기 - Clone() /// <summary> /// 복제하기 /// </summary> /// <returns>복제 객체</returns> public Student Clone() { return DeepCopy(); } #endregion #region 복제하기 - ICloneable.Clone() /// <summary> /// 복제하기 /// </summary> /// <returns>복제 객체</returns> object ICloneable.Clone() { return Clone(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 객체 DEEP 복사하기 - DeepCopy() /// <summary> /// 객체 DEEP 복사하기 /// </summary> /// <returns>복사 객체</returns> protected virtual Student DeepCopy() { Student student = new Student(); student.ID = ID; student.Name = string.Copy(Name); student.School = School.Clone(); return student; } #endregion } } |
▶ Program.cs
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 32 33 34 35 36 37 38 39 40 41 |
using System; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 실행하기 - Main() /// <summary> /// 프로그램 실행하기 /// </summary> private static void Main() { School school1 = new School(); school1.ID = 1; school1.Name = "동북고"; Student student1 = new Student(); student1.ID = 1; student1.Name = "홍길동"; student1.School = school1; Student student2 = student1.Clone(); Console.WriteLine(student1.School == student2.School); } #endregion } } |
TestProject.zip
■ 리플렉션 박싱없이 비공개 필드 값을 구하는 방법을 보여준다. ▶ Program.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
using System; using System.Reflection; using System.Reflection.Emit; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 값 구하기 대리자 - GetValueDelegate<T>(sourceInstance) /// <summary> /// 값 구하기 대리자 /// </summary> /// <typeparam name="T">값 타입</typeparam> /// <param name="sourceInstance">소스 인스턴스</param> /// <returns>값</returns> private delegate T GetValueDelegate<T>(object sourceInstance); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 값 /// </summary> private int value = 5; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.Title = "리플렉션 박싱없이 비공개 필드 값 구하기"; Program program = new Program(); program.value = 10; int value = GetNonPublicFieldValue<int>(program, "value"); Console.WriteLine("value 속성 : {0}", value); } #endregion #region 비공개 필드 값 구하기 - GetNonPublicFieldValue<TField>(sourceInstance, fieldName) /// <summary> /// 비공개 필드 값 구하기 /// </summary> /// <typeparam name="TField">필드 값 타입</typeparam> /// <param name="sourceInstance">소스 인스턴스</param> /// <param name="fieldName">필드명</param> /// <returns>필드 값</returns> private static TField GetNonPublicFieldValue<TField>(object sourceInstance, string fieldName) { FieldInfo fieldInfo = typeof(Program).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); DynamicMethod dynamicMethod = new DynamicMethod ( Guid.NewGuid().ToString(), typeof(TField), new Type[] { typeof(object) }, sourceInstance.GetType(), false ); ILGenerator generator = dynamicMethod.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldfld, fieldInfo); generator.Emit(OpCodes.Ret); GetValueDelegate<TField> getValueDelegate = (GetValueDelegate<TField>)dynamicMethod.CreateDelegate(typeof(GetValueDelegate<TField>)); TField result = getValueDelegate(sourceInstance); return result; } #endregion } } |
TestProject.zip
■ 다이나믹 객체 JSON 직렬화/역직렬화하는 방법을 보여준다. ▶ DynamicHelper.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
using System.Collections.Generic; using System.Dynamic; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace TestProject { /// <summary> /// 다이나믹 헬퍼 /// </summary> public static class DynamicHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region JSON 문자열 구하기 - GetJSON(sourceObject) /// <summary> /// JSON 문자열 구하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>JSON 문자열</returns> public static string GetJSON(object sourceObject) { string json = JsonConvert.SerializeObject(sourceObject); return json; } #endregion #region 다이나믹 객체 구하기 - GetDynamicObject(json) /// <summary> /// 다이나믹 객체 구하기 /// </summary> /// <param name="json">JSON 문자열</param> /// <returns>다이나믹 객체</returns> public static dynamic GetDynamicObject(string json) { object targetObject = JsonConvert.DeserializeObject(json); if(targetObject is string) { return targetObject as string; } else { return GetDynamicObject(targetObject as JToken); } } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 다이나믹 객체 구하기 - GetDynamicObject(token) /// <summary> /// 다이나믹 객체 구하기 /// </summary> /// <param name="token">토큰</param> /// <returns>다이나믹 객체</returns> private static dynamic GetDynamicObject(JToken token) { if(token is JValue) { return (token as JValue).Value; } else if(token is JObject) { ExpandoObject expandoObject = new ExpandoObject(); (from childToken in token where childToken is JProperty select childToken as JProperty).ToList().ForEach ( property => { ((IDictionary<string, object>)expandoObject).Add(property.Name, GetDynamicObject(property.Value)); } ); return expandoObject; } else if(token is JArray) { List<ExpandoObject> list = new List<ExpandoObject>(); foreach(JToken item in token as JArray) { list.Add(GetDynamicObject(item)); } return list; } return null; } #endregion } } |
▶ Program.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.Collections.Generic; namespace TestProject { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { dynamic object1 = new { ID = "AAA", NAME = "홍길동" }; dynamic object2 = new { ID = "BBB", NAME = "김연수" }; List<dynamic> list1 = new List<dynamic>(); list1.Add(object1); list1.Add(object2); string json = DynamicHelper.GetJSON(list1); Console.WriteLine("--------------------------------------------------"); Console.WriteLine(json); Console.WriteLine("--------------------------------------------------"); dynamic list2 = DynamicHelper.GetDynamicObject(json); Console.WriteLine(list2[0].NAME); Console.WriteLine(list2[1].NAME); } #endregion } } |
TestProject.zip
■ 다이나믹 객체 속성명의 포함 여부를 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System.Collections.Generic; #region 속성명 포함 여부 구하기 - ContainsPropertyName(sourceObject, propertyName) /// <summary> /// 속성명 포함 여부 구하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <param name="propertyName">속성명</param> /// <returns>속성명 포함 여부</returns> public bool ContainsPropertyName(dynamic sourceObject, string propertyName) { return (sourceObject as IDictionary<string, object>).ContainsKey(propertyName); } #endregion |
■ 객체 이니셜라이저를 사용하는 방법을 보여준다. ▶ 예제 코드 (XAML)
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 32 |
/// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name { get; set; } #endregion } ... Employee employee = new Employee { ID = "1", Name = "홍길동" }; |