Here is some code that will do what you want (I think), but I have to ask - why do you want to serialize such a string?
If the class is simple enough to serialize to a string, use an XML serializer that is much easier to handle; if you want to serialize it to disk, the binary writes it to a file, and if it is complex and you serialize it for transfer - think about using something like protobuf-net.
I think the essence of your problem is that you are trying to use ASCII encoding - I am using Base64 encoding.
Anyway - here it goes (I just guessed in your search class!)
class Program { [Serializable] public class Search { public Guid ID { get; private set; } public Search() { } public Search(Guid id) { ID = id; } public override string ToString() { return ID.ToString(); } } static void Main(string[] args) { Search search = new Search(Guid.NewGuid()); Console.WriteLine(search); string serialized = SerializeTest.SerializeToString(search); Search rehydrated = SerializeTest.DeSerializeFromString<Search>(serialized); Console.WriteLine(rehydrated); Console.ReadLine(); } } public class SerializeTest { public static Encoding _Encoding = Encoding.Unicode; public static string SerializeToString(object obj) { byte[] byteArray = BinarySerializeObject(obj); return Convert.ToBase64String(byteArray); } public static T DeSerializeFromString<T>(string input) { byte[] byteArray = Convert.FromBase64String(input); return BinaryDeserializeObject<T>(byteArray); }
NTN
source share