Having this class:
namespace CoolNamespace
{
[Serializable]
public class MyClass
{
public string Data { get; set; }
public int Age { get; set; }
}
}
In the class Program.cs from a console application, I am serializing it.
namespace TestSerialization
{
class Program
{
static void Main(string[] args)
{
MyClass c = new MyClass();
c.Data = "test";
c.Age = 6;
byte[] s = Serialize(c);
File.WriteAllBytes(@"C:\Temp\test.txt", s);
}
public static byte[] Serialize(object data)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
using (MemoryStream memoryStream = new MemoryStream())
{
new BinaryFormatter().Serialize((Stream)memoryStream, data);
memoryStream.Seek(0L, SeekOrigin.Begin);
return memoryStream.GetBuffer();
}
}
}
}
But I don't understand wht in the serialized class I am seeing the namespace of the class Program.cs? here is the serialized class:
ÿÿÿÿ HTestSerialization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null CoolNamespace.MyClass
k__BackingFieldk__BackingField test
So if I want to deserialize it in another namespace, it would fail with th error:
System.Runtime.Serialization.SerializationException: Unable to find assembly 'TestSerialization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
So how can I avoid this issue?