public static void LoadClassInstanceProperties(CodeGenerateSystem.Base.GeneratorClassBase classInstance, EngineNS.IO.XndAttrib att) { if (classInstance == null) { return; } int count; att.Read(out count); var classType = classInstance.GetType(); for (int i = 0; i < count; i++) { string proName; att.Read(out proName); string typeSaveStr; att.Read(out typeSaveStr); var type = EngineNS.Rtti.RttiHelper.GetTypeFromSaveString(typeSaveStr); var serializer = EngineNS.IO.Serializer.TypeDescGenerator.Instance.GetSerializer(type); if (serializer == null) { throw new InvalidOperationException($"读取属性失败,{proName}({type.FullName})找不到对应的序列化器"); } var val = serializer.ReadValue(att); var pro = classType.GetProperty(proName); if (pro == null) { continue; } if (pro.PropertyType != type) { continue; } pro.SetValue(classInstance, val); } }
public static void SaveClassInstanceProperties(CodeGenerateSystem.Base.GeneratorClassBase classInstance, EngineNS.IO.XndAttrib att) { if (classInstance == null) { return; } var pros = classInstance.GetType().GetProperties(); att.Write((int)pros.Length); foreach (var pro in pros) { att.Write(pro.Name); var typeSaveStr = EngineNS.Rtti.RttiHelper.GetTypeSaveString(pro.PropertyType); att.Write(typeSaveStr); var serializer = EngineNS.IO.Serializer.TypeDescGenerator.Instance.GetSerializer(pro.PropertyType); if (serializer == null && pro.PropertyType.IsInterface == false) { throw new InvalidOperationException($"保存属性失败,{pro.Name}({pro.PropertyType.FullName})找不到对应的序列化器"); } var infoDesc = new EngineNS.Rtti.PropMemberDesc(pro); var val = pro.GetValue(classInstance); serializer.WriteValue(val, att); } }