Advertisement
Guest User

Serializer

a guest
Nov 14th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.IO;
  7.  
  8. namespace NativeSerializer
  9. {
  10.     static class NativeSerializer
  11.     {
  12.         public static void Serialize(Stream stream, object obj)
  13.         {
  14.             // get the fields
  15.             Type type = obj.GetType();
  16.             FieldInfo[] fiList = type.GetFields();
  17.             // set up a writer
  18.             var bw = new BinaryWriter(stream);
  19.             // process each field
  20.             foreach (FieldInfo fi in fiList)
  21.             {
  22.                 // array?
  23.                 if (fi.FieldType.Name.EndsWith("[]"))
  24.                 {
  25.                     // get the array element type
  26.                     Type elementType = Type.GetType(fi.FieldType.FullName.TrimEnd('[', ']'));
  27.                     // pull out the value
  28.                     var array = (Array)fi.GetValue(obj);
  29.                     // write the length of the array
  30.                     bw.Write(array.Length);
  31.                     // write all of the data
  32.                     for (int i = 0; i < array.Length; i++)
  33.                     {
  34.                         typeof(BinaryWriter)
  35.                             .GetMethod("Write", new Type[] { elementType })
  36.                             .Invoke(bw, new object[] { array.GetValue(i) });
  37.                     }
  38.                 }
  39.                 else
  40.                 {
  41.                     // write the value
  42.                     typeof(BinaryWriter)
  43.                         .GetMethod("Write", new Type[] { fi.FieldType })
  44.                         .Invoke(bw, new object[] { fi.GetValue(obj) });
  45.                 }
  46.             }
  47.         }
  48.  
  49.         public static T Deserialize<T>(Stream stream)
  50.         {
  51.             // get fields of the class
  52.             FieldInfo[] fiList = typeof(T).GetFields();
  53.             // create an instance
  54.             object obj = Activator.CreateInstance(typeof(T));
  55.             var br = new BinaryReader(stream);
  56.             // process each field
  57.             foreach (FieldInfo fi in fiList)
  58.             {
  59.                 // array?
  60.                 if (fi.FieldType.Name.EndsWith("[]"))
  61.                 {
  62.                     // get the element type
  63.                     Type elementType = Type.GetType(fi.FieldType.FullName.TrimEnd('[', ']'));
  64.                     // create the array instance based on the length specified
  65.                     var array = Array.CreateInstance(elementType, br.ReadInt32());
  66.                     // read out all the values and set the array
  67.                     for (int i = 0; i < array.Length; i++)
  68.                     {
  69.                         // set value for current index
  70.                         array.SetValue(
  71.                             typeof(BinaryReader)
  72.                                 .GetMethod("Read" + elementType.Name)
  73.                                 .Invoke(br, null),
  74.                             i
  75.                         );
  76.                     }
  77.                     // write the array back to the field
  78.                     fi.SetValue(obj, array);
  79.                 }
  80.                 else
  81.                 {
  82.                     // read and set the value
  83.                     fi.SetValue(obj,
  84.                         typeof(BinaryReader)
  85.                             .GetMethod("Read" + fi.FieldType.Name)
  86.                             .Invoke(br, null)
  87.                         );
  88.                 }
  89.             }
  90.             return (T)obj;
  91.         }
  92.     }
  93. }
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement