static void push_primitive(object obj, FieldInfo field, BuffBuilder bb) { if (field.FieldType == typeof(uint)) { bb.PushUint((uint)field.GetValue(obj)); } else if (field.FieldType == typeof(int)) { bb.PushInt((int)field.GetValue(obj)); } else if (field.FieldType == typeof(ushort)) { bb.PushUshort((ushort)field.GetValue(obj)); } else if (field.FieldType == typeof(short)) { bb.PushShort((short)field.GetValue(obj)); } else if (field.FieldType == typeof(ulong)) { bb.PushUlong((ulong)field.GetValue(obj)); } else if (field.FieldType == typeof(long)) { bb.PushLong((long)field.GetValue(obj)); } else if (field.FieldType == typeof(byte)) { bb.PushByte((byte)field.GetValue(obj)); } else if (field.FieldType == typeof(float)) { bb.PushFloat((float)field.GetValue(obj)); } }
static void push_class(object obj, BuffBuilder bb) { FieldInfo[] fields = obj.GetType().GetFields(); for (int i = 0; i < fields.Length; ++i) { if (fields[i].FieldType.IsPrimitive) { push_primitive(obj, fields[i], bb); } else if (fields[i].FieldType == typeof(string)) { uint length = (uint)fields[i - 1].GetValue(obj); string s = fields[i].GetValue(obj) as string; bb.PushString(s, (int)length); } else if (fields[i].FieldType.IsClass) { if (fields[i].FieldType.IsArray) { System.Type elemtype = fields[i].FieldType.GetElementType(); uint length = (uint)fields[i - 1].GetValue(obj); if (elemtype.IsClass) { object[] objs = (object[])fields[i].GetValue(obj); for (uint j = 0; j < length; ++j) { push_class(objs[j], bb); } } else if (elemtype.IsPrimitive) { push_primitive_array(obj, length, fields[i], bb); } } else { object sub_obj = fields[i].GetValue(obj); push_class(sub_obj, bb); } } } }
static void push_primitive_array(object obj, uint length, FieldInfo field, BuffBuilder bb) { System.Type elemtype = field.FieldType.GetElementType(); if (elemtype == typeof(uint)) { uint[] objs = (uint[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushUint(objs[j]); } } else if (elemtype == typeof(int)) { int[] objs = (int[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushInt(objs[j]); } } else if (elemtype == typeof(ushort)) { ushort[] objs = (ushort[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushUshort(objs[j]); } } else if (elemtype == typeof(short)) { short[] objs = (short[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushShort(objs[j]); } } else if (elemtype == typeof(ulong)) { ulong[] objs = (ulong[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushUlong(objs[j]); } } else if (elemtype == typeof(long)) { long[] objs = (long[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushLong(objs[j]); } } else if (elemtype == typeof(byte)) { byte[] objs = (byte[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushByte(objs[j]); } } else if (elemtype == typeof(float)) { float[] objs = (float[])field.GetValue(obj); for (uint j = 0; j < length; ++j) { bb.PushFloat(objs[j]); } } }
public static byte[] GenericsEncode(object obj) { BuffBuilder bb = new BuffBuilder(1024); push_class(obj, bb); return bb.GetBuff(); }