15 private Func<TagCompound, T> deserializer;
17 public TagSerializableSerializer() {
19 var field = type.GetField(
"DESERIALIZER");
21 if (field.FieldType != typeof(Func<TagCompound, T>))
22 throw new ArgumentException(
23 $
"Invalid deserializer field type {field.FieldType} in {type.FullName} expected {typeof(Func<TagCompound, T>)}.");
25 deserializer = (Func<TagCompound, T>)field.GetValue(
null);
29 public override TagCompound Serialize(T value) {
30 var tag = value.SerializeData();
31 tag[
"<type>"] = value.GetType().FullName;
35 public override T Deserialize(TagCompound tag) {
36 if (tag.ContainsKey(
"<type>") && tag.GetString(
"<type>") != Type.FullName) {
37 var instType = GetType(tag.GetString(
"<type>"));
39 if (instType !=
null && Type.IsAssignableFrom(instType) && TryGetSerializer(instType, out instSerializer))
40 return (T)instSerializer.Deserialize(tag);
43 if (deserializer ==
null)
44 throw new ArgumentException($
"Missing deserializer for type '{Type.FullName}'.");
46 return deserializer(tag);
TagCompound SerializeData()