public void Write(PPDPack.V2.PackWriter writer) { foreach (var propertyInfo in GetType().GetProperties()) { if (!propertyInfo.CanRead || propertyInfo.GetCustomAttributes(false).Any(a => a.GetType() == typeof(IgnoreAttribute))) { continue; } var value = propertyInfo.GetValue(this, null); if (propertyInfo.PropertyType.IsArray) { var array = (Array)value; var elementType = propertyInfo.PropertyType.GetElementType(); writer.Write(propertyInfo.Name, w => { Write(typeof(int), array.Length, w); for (int i = 0; i < array.Length; i++) { var arrayValue = array.GetValue(i); Write(elementType, arrayValue, w); } }); } else { writer.Write(propertyInfo.Name, w => { Write(propertyInfo.PropertyType, value, w); }); } } }
public void Send(PackableBase packable) { if (!connected || !tcpClient.Connected) { return; } try { using (MemoryStream stream = new MemoryStream()) { using (PPDPack.V2.PackWriter writer = new PPDPack.V2.PackWriter(stream)) { packable.Write(writer); } stream.Close(); Send(stream.ToArray()); } } catch (Exception e) { Console.WriteLine("Send Error: {0}", e.Message); Console.WriteLine("Send Error: {0}", e.StackTrace); } }