protected virtual void ToByteArrayAux(byte[] buffer, ref int i) { FieldInfo[] fields = this.GetType().GetFields(); foreach (FieldInfo field in fields) { Type tipo = field.FieldType; if (tipo == typeof(byte)) { buffer[i] = (byte)field.GetValue(this); i += 1; } else if (tipo == typeof(sbyte)) { sbyte tmp = (sbyte)field.GetValue(this); buffer[i] = (byte)tmp; i += 1; } else if (tipo == typeof(Int16)) { USBXpress.USBXpress.toarray(buffer, ref i, (Int16)field.GetValue(this)); //i += 2; } else if (tipo == typeof(Int32)) { USBXpress.USBXpress.toarray(buffer, ref i, (Int32)field.GetValue(this)); //i += 4; } else if (tipo == typeof(float)) { USBXpress.USBXpress.toarray(buffer, ref i, (float)field.GetValue(this)); //i += 4; } else if (tipo == typeof(string)) { string cad = (string)field.GetValue(this); for (int j = 0; j < 16; j++) { if (j < cad.Length) { buffer[i + j] = (byte)cad[j]; } else { buffer[i + j] = (byte)0; } } i += 16; } else if (tipo.IsSubclassOf(typeof(GenericConfigClass))) { GenericConfigClass tmp = (GenericConfigClass)field.GetValue(this); tmp.ToByteArrayAux(buffer, ref i); } } }
protected virtual void FromByteArrayAux(byte[] buffer, ref int i) { FieldInfo[] fields = this.GetType().GetFields(); foreach (FieldInfo field in fields) { Type tipo = field.FieldType; if (tipo == typeof(byte)) { field.SetValue(this, buffer[i]); i += 1; } else if (tipo == typeof(sbyte)) { sbyte tmp = (sbyte)buffer[i]; field.SetValue(this, tmp); i += 1; } else if (tipo == typeof(Int16)) { Int16 tmp = USBXpress.USBXpress.toint16(buffer, ref i); field.SetValue(this, tmp); //i += 2; } else if (tipo == typeof(Int32)) { Int32 tmp = USBXpress.USBXpress.toint32(buffer, ref i); field.SetValue(this, tmp); //i += 4; } else if (tipo == typeof(float)) { float tmp = USBXpress.USBXpress.tofloat(buffer, ref i); field.SetValue(this, tmp); //i += 4; } else if (tipo == typeof(string)) { string cad = ""; for (int j = 0; j < 16 && buffer[i + j] != 0; j++) { cad += (char)buffer[i + j]; } field.SetValue(this, cad); i += 16; } else if (tipo.IsSubclassOf(typeof(GenericConfigClass))) { GenericConfigClass tmp = (GenericConfigClass)Activator.CreateInstance(tipo); tmp.FromByteArrayAux(buffer, ref i); field.SetValue(this, tmp); } } }
public virtual int size_bytes() { int size = 0; FieldInfo[] fields = GetType().GetFields(); foreach (FieldInfo field in fields) { Type tipo = field.FieldType; if (tipo == typeof(byte)) { size += 1; } else if (tipo == typeof(sbyte)) { size += 1; } else if (tipo == typeof(Int16)) { size += 2; } else if (tipo == typeof(Int32)) { size += 4; } else if (tipo == typeof(float)) { size += 4; } else if (tipo == typeof(string)) { size += 16; } else if (tipo.IsSubclassOf(typeof(GenericConfigClass))) { GenericConfigClass tmp = (GenericConfigClass)Activator.CreateInstance(tipo); size += tmp.size_bytes(); } else { size += 0; } } return(size); }