public ValueField(BinaryReader BR) { Name = BR.ReadString(); FieldType = Type.GetType(BR.ReadString()); if (FieldType == typeof(string)) { Value = BR.ReadString(); } else if (FieldType == typeof(int)) { Value = BR.ReadInt32(); } else if (FieldType == typeof(bool)) { Value = BR.ReadBoolean(); } else if (FieldType == typeof(float)) { Value = BR.ReadSingle(); } else { if (FieldType.GetInterface(nameof(IValueSerializable)) != null) { bool IsNull = BR.ReadBoolean(); if (IsNull) { Value = null; } else { IValueSerializable ValInstance = (IValueSerializable)Activator.CreateInstance(FieldType); ValInstance.Deserialize(BR); Value = ValInstance; } return; } throw new Exception("Unknown type"); } }
public void Serialize(BinaryWriter BW) { BW.Write(Name); BW.Write(FieldType.AssemblyQualifiedName); if (FieldType == typeof(string)) { BW.Write((string)Value); } else if (FieldType == typeof(int)) { BW.Write((int)Value); } else if (FieldType == typeof(bool)) { BW.Write((bool)Value); } else if (FieldType == typeof(float)) { BW.Write((float)Value); } else { if (FieldType.GetInterface(nameof(IValueSerializable)) != null) { IValueSerializable ValInstance = (IValueSerializable)Value; if (ValInstance == null) { BW.Write(true); } else { BW.Write(false); ValInstance.Serialize(BW); } return; } throw new Exception("Unknown type"); } }