public static TypeDesciption Parse(Type type) { TypeDesciption td = new TypeDesciption(); td.type = type; td.Length = 0; foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { if (field.FieldType.IsEnum) { var enum_type = Enum.GetUnderlyingType(field.FieldType); ITypeBitConverter tbc; if (!typeDescriptions.TryGetValue(enum_type, out tbc)) { throw new Exception("Unknown enum underlying type '{0}'".format(enum_type.FullName)); } td.fields.Add(Tuple.Create(field, tbc)); td.Length += tbc.Length; } else { ITypeBitConverter tbc; if (!typeDescriptions.TryGetValue(field.FieldType, out tbc)) { tbc = TypeDesciption.Parse(field.FieldType); typeDescriptions.Add(field.FieldType, tbc); } td.fields.Add(Tuple.Create(field, tbc)); td.Length += tbc.Length; } } return(td); }
public static byte[] GetBytes(this object o) { Type type = o.GetType(); ITypeBitConverter tbc; if (!typeDescriptions.TryGetValue(type, out tbc)) { tbc = TypeDesciption.Parse(type); typeDescriptions.Add(type, tbc); } return(tbc.GetBytes(o)); }
public static void RegisterType(Type type) { ITypeBitConverter tbc; if (!typeDescriptions.TryGetValue(type, out tbc)) { tbc = TypeDesciption.Parse(type); typeDescriptions.Add(type, tbc); } else { Log.Error("Type '{0}' is already registered in BitConverterEx.", type.FullName); } }
public static object FromBytes(Type type, byte[] value, ref int startIndex) { ITypeBitConverter tbc; if (!typeDescriptions.TryGetValue(type, out tbc)) { tbc = TypeDesciption.Parse(type); typeDescriptions.Add(type, tbc); } object o = tbc.FromBytes(value, startIndex); startIndex += tbc.Length; return(o); }