using System; using System.Collections.Generic; using System.Linq; using System.Text; using Esiur.Misc; using System.Reflection; namespace Esiur.Data { /// /// BinaryList holds a list of items to be converted to binary for storage and transmission /// public class BinaryList { private List held = new List(); /// /// Create an instance of BinaryList /// public BinaryList() { } /// /// Converts parameters to binary in same order /// /// Variables to convert public static byte[] ToBytes(params object[] values) { var held = new List(); foreach (var i in values) { if (i is byte) held.Add((byte)i); else { #if NETSTANDARD1_5 MethodInfo mi = typeof(DC).GetTypeInfo().GetMethod("ToBytes", new Type[] { i.GetType() }); #else MethodInfo mi = typeof(DC).GetMethod("ToBytes", new Type[] { i.GetType() }); #endif if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] { i }); held.AddRange(b); } } } return held.ToArray(); } /// /// Create a new instance of BinaryList /// /// Populate the list items public BinaryList(params object[] values) { AddRange(values); } /// /// Add an array of items at the end of the list /// /// Array of items public void AddRange(object[] values) { foreach (var i in values) { if (i is byte) held.Add((byte)i); else { #if NETSTANDARD1_5 MethodInfo mi = typeof(DC).GetTypeInfo().GetMethod("ToBytes", new Type[] { i.GetType() }); #else MethodInfo mi = typeof(DC).GetMethod("ToBytes", new Type[] { i.GetType() }); #endif if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] {i}); held.AddRange(b); } } } } /// /// Add multiple items at the end of the list /// /// Parameters of items public void Append(params object[] values) { AddRange(values); } /// /// Insert new items to the list at a specified index /// /// Position in the list /// Items to insert public void Insert(int offset, params object[] values) { foreach (var i in values) { if (i is byte) { held.Insert(offset++, (byte)i); } else { #if NETSTANDARD1_5 MethodInfo mi = typeof(DC).GetTypeInfo().GetMethod("ToBytes", new Type[] { i.GetType() }); #else MethodInfo mi = typeof(DC).GetMethod("ToBytes", new Type[] { i.GetType() }); #endif if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] { i }); held.InsertRange(offset, b); offset += b.Length; } } } } /// /// Number of the items in the list /// public int Length { get { return held.Count; } } /* public void Append(byte data) { held.Add(data); } public void Append(byte[] data) { held.AddRange(data); } public void Append(int data) { held.AddRange(DC.ToBytes(data)); } public void Append(uint data) { held.AddRange(DC.ToBytes(data)); } public void Append(float data) { held.AddRange(DC.ToBytes(data)); } public void Append(short data) { held.AddRange(DC.ToBytes(data)); } public void Append(ushort data) { held.AddRange(DC.ToBytes(data)); } public void Append(double data) { held.AddRange(DC.ToBytes(data)); } public void Append(sbyte data) { held.Add((byte)data); } */ /// /// Convert the list to an array of bytes /// /// Bytes array public byte[] ToArray() { return held.ToArray(); } } }