[C#/WINFORM/DEVEXPRESS] PivotGridOptionsData 클래스 : CustomObjectConverter 속성을 사용해 객체 직렬화 커스텀 설정하기
■ PivotGridOptionsData 클래스의 CustomObjectConverter 속성을 사용해 객체 직렬화를 커스텀 설정하는 방법을 보여준다. ▶ CustomObjectConverter.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 106 |
using System; using DevExpress.Utils.Serializing.Helpers; namespace TestProject { /// <summary> /// 커스텀 객체 컨버터 /// </summary> public class CustomObjectConverter : ICustomObjectConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환 가능 여부 구하기 - CanConvert(type) /// <summary> /// 변환 가능 여부 구하기 /// </summary> /// <param name="type">타입</param> /// <returns>변환 가능 여부</returns> public bool CanConvert(Type type) { return type == typeof(Employee); } #endregion #region 문자열에서 객체 구하기 - FromString(type, source) /// <summary> /// 문자열에서 객체 구하기 /// </summary> /// <param name="type">타입</param> /// <param name="source">소스</param> /// <returns>객체</returns> public object FromString(Type type, string source) { if(type != typeof(Employee)) { return null; } string[] itemArray = source.Split('#'); if(itemArray.Length >= 3) { return new Employee(itemArray[0], itemArray[1], int.Parse(itemArray[2])); } else if(itemArray.Length == 2) { return new Employee(itemArray[0], itemArray[1], 0); } else if(itemArray.Length == 1) { return new Employee(itemArray[0], string.Empty, 0); } else { return new Employee(string.Empty, string.Empty, 0); } } #endregion #region 문자열 구하기 - ToString(type, source) /// <summary> /// 문자열 구하기 /// </summary> /// <param name="type">타입</param> /// <param name="source">소스</param> /// <returns>문자열</returns> public string ToString(Type type, object source) { if(type != typeof(Employee)) { return string.Empty; } Employee employee = source as Employee; return employee.FirstName + '#' + employee.LastName + '#' + employee.Age; } #endregion #region 타입 구하기 - GetType(typeName) /// <summary> /// 타입 구하기 /// </summary> /// <param name="typeName">타입명</param> /// <returns>타입</returns> public Type GetType(string typeName) { if(typeName != typeof(Employee).FullName) { return null; } return typeof(Employee); } #endregion } } |
▶ MainForm.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 |
using System; using System.IO; using DevExpress.XtraEditors; using DevExpress.Data.PivotGrid; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 스트림 /// </summary> private MemoryStream stream; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.pivotGridControl.DataSource = Employee.GetDataTable(); this.pivotGridControl.OptionsData.CustomObjectConverter = new CustomObjectConverter(); this.saveButton.Click += saveButton_Click; this.loadButton.Click += loadButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Save 버튼 클릭시 처리하기 - saveButton_Click(sender, e) /// <summary> /// Save 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveButton_Click(object sender, EventArgs e) { if(this.stream != null) { this.stream.Dispose(); } this.stream = new MemoryStream(); this.pivotGridControl.SavePivotGridToStream(stream); } #endregion #region Load 버튼 클릭시 처리하기 - loadButton_Click(sender, e) /// <summary> /// Load 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void loadButton_Click(object sender, EventArgs e) { if(this.stream == null) { return; } PivotFileDataSource dataSource = new PivotFileDataSource(this.stream, new CustomObjectConverter()); this.pivotGridControl.DataSource = dataSource; } #endregion } } |
TestProject.zip