[C#/MESSAGEPACK] MessagePackSerializer 클래스 : Serialize/Deserialize 정적 메소드를 사용해 직렬화하기
C#/MessagePack 2020. 11. 17. 14:48728x90
반응형
728x170
▶ Employee.cs
using System.Collections.Generic;
using MessagePack;
namespace TestProject
{
/// <summary>
/// 직원
/// </summary>
[MessagePackObject]
public class Employee
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region ID - ID
/// <summary>
/// ID
/// </summary>
[Key(0)]
public virtual int ID { get; set; }
#endregion
#region 성명 - Name
/// <summary>
/// 성명
/// </summary>
[Key(1)]
public virtual string Name { get; set; }
#endregion
#region 부서 - Department
/// <summary>
/// 부서
/// </summary>
[Key(2)]
public virtual string Department { get; set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 리스트 구하기 - GetList(count)
/// <summary>
/// 리스트 구하기
/// </summary>
/// <param name="count">카운트</param>
/// <returns>리스트</returns>
public static List<Employee> GetList(int count)
{
List<Employee> list = new List<Employee>();
for(int i = 0; i < count; i++)
{
list.Add(new Employee { ID = i + 1, Name = $"직원 {i + 1}", Department = $"{i % 4 + 1}" });
}
return list;
}
#endregion
}
}
728x90
▶ Program.cs
using System;
using System.Collections.Generic;
using MessagePack;
namespace TestProject
{
/// <summary>
/// 프로그램
/// </summary>
class Program
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 프로그램 시작하기 - Main()
/// <summary>
/// 프로그램 시작하기
/// </summary>
private static void Main()
{
Console.Title = "MessagePackSerializer 클래스 : Serialize/Deserialize 정적 메소드를 사용해 직렬화하기";
List<Employee> sourceList = Employee.GetList(10);
byte[] byteArray = MessagePackSerializer.Serialize<List<Employee>>(sourceList);
List<Employee> targetList = MessagePackSerializer.Deserialize<List<Employee>>(byteArray);
foreach(Employee employee in targetList)
{
Console.WriteLine(employee.Name);
}
}
#endregion
}
}
※ 패키지 설치 : MessagePack
1. 비주얼 스튜디오를 실행한다.
2. 비주얼 스튜디오에서 [도구] / [NuGet 패키지 관리자] / [패키지 관리자 콘솔] 메뉴를 클릭한다.
3. [패키지 관리자 콘솔]에서 아래 명령을 실행한다.
Install-Package MessagePack
728x90
반응형
그리드형(광고전용)
'C# > MessagePack' 카테고리의 다른 글
[C#/MESSAGEPACK] MessagePackSerializer 클래스 : Serialize/Deserialize 정적 메소드를 사용해 직렬화하기 (0) | 2020.11.17 |
---|---|
[C#/MESSAGEPACK] 누겟 설치 : MessagePack (0) | 2020.11.17 |
댓글을 달아 주세요