using System;using System.Collections;using System.IO;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;
[Serializable]public class MyClass{ private string _Value; public string Value { get { return _Value; } set { _Value = value; } } public static void Main() { int[] lens = new int[] {100, 800, 1000, 3000, 6000, 10000}; MyClass myClass = new MyClass(); foreach (int len in lens) { myClass.Value = new string('z', len); byte[] buff = ObjectToBytes(myClass); WL("Field Length: {0}, Buffer Length: {1}", len, buff.Length); } RL(); } public static byte[] ObjectToBytes(object obj) { using (MemoryStream ms = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.GetBuffer(); } }