Goal
- Save current the dynamic object to file
- Load the save and set it
I search and found the below method. but it's for serializable field/class.
For Serializable
public byte[] ToByteArray<T>(T obj)
{
BinaryFormatter b = new BinaryFormatter()
using(MemoryStream m = new MemoryStream)
{
b.Serialize(m,obj);
return ms.ToArray();
}
}
public T FromByteArray<T>(byte[] data)
{
BinaryFormatter b = new BinaryFormatter()
using(MemoryStream m = new MemoryStream)
{
object obj = b.Deserialize(m);
return (T)obj;
}
}
Goal Example
The dynamic d can't be a serializable class because it hasn't only x,y and z. My goal is creating two functions: Save a list of dynamic to file and Load it from file.
- bool ToFile(List list, string where)
- List Load(string where)
List<dynamic> save = new List<dynamic>();
dynamic d = new ExpandoObject();
d.x = 1;
d.y = 3;
save.add(d);
d.z = 5;
d._type = "1";
save.add(d);
ToFile(save,"C:/Save.byte");
List<dynamic> load = Load("C:/Save.byte");
for(int I = 0; I < load.Count; I++)
{
print("Round " + I);
if(load[I].z != null)
print(load[I].z);
}
Console
Round 1
Round 2
5