public void WriteType(object obj, Configuration configuration, BinaryWritingContext context) { try { var givenType = obj.GetType(); var typeDescription = configuration.Descriptions.Find(p => p.ClassName == givenType.AssemblyQualifiedName); if (typeDescription == null) { throw new ArgumentException("Cannot find description for given object of class " + givenType.AssemblyQualifiedName); } foreach (var fieldDescription in typeDescription.Fields) { var typeWriter = TypeWriterFactory.GetTypeWriter(fieldDescription.Type, configuration); var fieldObject = givenType.GetProperty(fieldDescription.Name).GetGetMethod().Invoke(obj, new object[] {}); typeWriter.WriteType(fieldObject, configuration, context); } } catch (Exception exception) { throw new InvalidOperationException( "Cannot write type to given context", exception); } }
public void WriteType(Object obj, Configuration configuration, BinaryWritingContext context) { Type type = obj.GetType(); MethodInfo lengthProperty = type.GetProperty("Length").GetGetMethod(); var arrayLength = (int)lengthProperty.Invoke(obj, new object[] { }); MethodInfo getValueMethod = type.GetMethod("GetValue", new[] { Type.GetType("System.Int32") }); for (int i = 0; i < arrayLength; i++) { var propValue = getValueMethod.Invoke(obj, new object[] { i }); var typeWriter = TypeWriterFactory.GetTypeWriter(propValue,configuration); typeWriter.WriteType(propValue,configuration,context); } }
private void WriteChar(Object obj, BinaryWritingContext context) { try { if (obj is char) { var bytes = BitConverter.GetBytes((char)obj); Array.Reverse(bytes); context.WriteBytes(bytes); } else throw new InvalidOperationException("Passed object is not a char"); } catch (Exception exception) { throw new InvalidOperationException("Cannot write char to given context", exception); } }
public void WriteType(Object obj, Configuration configuration, BinaryWritingContext context) { switch (obj.GetType().FullName) { case "System.Byte": WriteByte(obj,context); break; case "System.Char": WriteChar(obj, context); break; case "System.Double": WriteDouble(obj, context); break; case "System.Single": WriteFloat(obj, context); break; case "System.Int64": WriteLong(obj, context); break; case "System.Int16": WriteShort(obj, context); break; case "System.String": WriteString(obj, context); break; case "System.Int32": WriteInt(obj, context); break; case "System.UInt64": WriteULong(obj, context); break; case "System.UInt16": WriteUShort(obj, context); break; case "System.UInt32": WriteUInt(obj,context); break; default: throw new ArgumentException("Unsupported simple type: "+obj.GetType().FullName); } }
private void WriteString(object obj, BinaryWritingContext context) { try { if (obj is string) { var str = (string)obj; foreach (var ch in str) { context.WriteBytes(BitConverter.GetBytes(ch)); } } else throw new InvalidOperationException("Passed object is not a string"); } catch (Exception exception) { throw new InvalidOperationException("Cannot write string to given context", exception); } }
private void WriteShort(object obj, BinaryWritingContext context) { try { if (obj is short) { context.WriteBytes(BitConverter.GetBytes((short)obj)); } else throw new InvalidOperationException("Passed object is not a short"); } catch (Exception exception) { throw new InvalidOperationException("Cannot write short to given context", exception); } }