Click here to Skip to main content
12,747,366 members (34,466 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 23 May 2014

Easy Seralization Using Extension Methods

, 23 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Easy seralization by adding extension methods to generic class


Binary serialization is very common in some scenarios, so, this tip adds this functionality as extension methods in order to provide an easy and comfortable way to use it.


You must know C#, serialization concept and extension methods.

Using the Code

The extension methods are as given below:

public static class Extensions
    public static void Serialize<T>(this T obj, string filePath=null)
        filePath = filePath ?? (filePath ?? (obj.GetType().GenericTypeArguments[0].FullName + ".bin"));

        using (Stream stream = File.Open(filePath, FileMode.Create))
            BinaryFormatter bin = new BinaryFormatter();
            bin.Serialize(stream, obj);

    public static T DeSerialize<T>(this T obj, string filePath=null)
        T result;
        filePath = filePath ?? (filePath ?? (obj.GetType().GenericTypeArguments[0].FullName + ".bin"));

        if (!File.Exists(filePath)){
            return (obj);

        using (Stream stream = File.Open(filePath, FileMode.Open)){
            BinaryFormatter bin = new BinaryFormatter();
            result = (T)bin.Deserialize(stream);

        return (result);

Sample of use is as follows:

public class Person
    public string Name { get; set; }
    public int Age { get; set; }

private void testSerialization()
    //Instance a new list and get it from file (if exists)
    List<Person> lstPerson = new List<Person>().DeSerialize();

    //If list is void, fill and save it (serialize)
    if (lstPerson.Count() == 0){
        lstPerson.Add(new Person() { Name = "Pepe", Age = 60 });
        lstPerson.Add(new Person() { Name = "Juan", Age = 70 });

    //check list content
    foreach (var person in lstPerson){
        Console.WriteLine("{0} {1}", person.Name, person.Age);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Volynsky Alex23-May-14 13:21
professionalVolynsky Alex23-May-14 13:21 
QuestionComment Pin
FatCatProgrammer23-May-14 9:50
memberFatCatProgrammer23-May-14 9:50 
AnswerRe: Comment Pin
meula23-May-14 22:05
membermeula23-May-14 22:05 
GeneralRe: Comment Pin
FatCatProgrammer30-May-14 7:47
memberFatCatProgrammer30-May-14 7:47 
GeneralRe: Comment Pin
meula30-May-14 8:57
membermeula30-May-14 8:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170215.1 | Last Updated 23 May 2014
Article Copyright 2014 by meula
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid