/// <summary> 加载类型文件完成 | Load type file complete </summary> private void OnLoadTypeComplete(byte[] bytes) { // 分析类型文件 | Analyze type file string code = Encoding.UTF8.GetString(bytes); List <LBToken> tokens = LBLexer.Analyze(code); LBReport report = LBParser.Analyze(code, tokens); if (report.IsSuccess) { LBConfig.AddCustomType(report.Type); if (fileNames.Count > 0) { LoadNextTypeFile(); } else { LBConfig.CheckAllCustomTypes(); onCompleteCallback(); } } else { throw new Exception("Type file analyzing failed\n" + report.Error); } }
/// <summary> 解析基本类型简称 | Parse base type short name </summary> private void ParseBaseTypeShortNames(ShortNameVo[] shortNames) { for (int i = 0; i < shortNames.Length; i++) { ShortNameVo shortName = shortNames[i]; LBConfig.AddBaseTypeShortName(shortName.type, shortName.name); } }
/// <summary> 反序列化 | Deserialize(Parse bytes to object) </summary> /// <param name="typeName">自定义类型名称 | Custom type name</param> /// <param name="bytes">字节数组 | Byte array</param> public static T Deserialize <T>(string typeName, byte[] bytes) where T : new() { LBType customType; if (!LBConfig.TryGetCustomType(typeName, out customType)) { throw new Exception("CustomType is null. Type name:" + typeName); } return(LBConverter.ToObject <T>(customType, bytes)); }
/// <summary> 序列化(对象转字节数组) | Serialize(Convert object to bytes) </summary> /// <param name="typeName">自定义类型名称 | Custom type name</param> /// <param name="obj">对象(类或结构) | object(class or struct)</param> public static byte[] Serialize(string typeName, object obj) { LBType customType; if (!LBConfig.TryGetCustomType(typeName, out customType)) { throw new Exception("CustomType is null. Type name:" + typeName); } return(LBConverter.ToBytes(customType, obj)); }