Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Archives
Today
Total
관리 메뉴

게임 프로그래밍

[c#] BinaryFormatter 본문

프로그래밍/C#

[c#] BinaryFormatter

Junwe 2020. 1. 14. 22:24
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Reflection;
using System;

public class SerializeObject
{
 public static void Serialize<Object>(Object dictionary, Stream stream)
    {
        try // try to serialize the collection to a file
        {
            using (stream)
            {
                // create BinaryFormatter
                BinaryFormatter bin = new BinaryFormatter();
                // serialize the collection (EmployeeList1) to file (stream)
                bin.Serialize(stream, dictionary);
            }
        }
        catch (IOException)
        {
        }
    }

    public static Object Deserialize<Object>(Stream stream) where Object : new()
    {
        Object ret = CreateInstance<Object>();
        try
        {
            using (stream)
            {
                // create BinaryFormatter
                BinaryFormatter bin = new BinaryFormatter();
                bin.Binder = new VersionDeserializer();
                // deserialize the collection (Employee) from file (stream)
                ret = (Object)bin.Deserialize(stream);
            }
        }
        catch (IOException)
        {
        }
        return ret;
    }
    // function to create instance of T
    public static Object CreateInstance<Object>() where Object : new()
    {
        return (Object)Activator.CreateInstance(typeof(Object));
    }
}

 

 

클래스를 BinaryFormatter을 이용해서 저장하고 불러오는 코드. 굳이 설명은 필요 없을듯

 

'프로그래밍 > C#' 카테고리의 다른 글

[C#] is, as에 대해서  (0) 2020.03.01
[C#] params 가변인자  (0) 2020.03.01
[C#] CSV 파일 저장하기  (0) 2020.02.19
C# &&연산자 팁  (0) 2016.04.25
c# 문자열 합치기  (0) 2015.09.27
Comments