public void LittleEndian( Type type, object writeValue, int alignment, byte[] bigEndianData, byte[] littleEndianData) { // via Delegate { MessageWriter writer = new MessageWriter(EndianFlag.Little); var method = s_createWriteDelegateMethod.MakeGenericMethod(type); var writeMethodDelegate = (Delegate)method.Invoke(null, null); writeMethodDelegate.DynamicInvoke(new object[] { writer, writeValue }); var bytes = writer.ToArray(); Assert.Equal(littleEndianData, bytes); } // via WriteMethod { MessageWriter writer = new MessageWriter(EndianFlag.Little); var writeMethodInfo = WriteMethodFactory.CreateWriteMethodForType(type, true); if (writeMethodInfo.IsStatic) { writeMethodInfo.Invoke(null, new object[] { writer, writeValue }); } else { writeMethodInfo.Invoke(writer, new object[] { writeValue }); } var bytes = writer.ToArray(); Assert.Equal(littleEndianData, bytes); } }
public void Invalid(Type type) { var method = s_createWriteDelegateMethod.MakeGenericMethod(type); Exception exception = null; try { method.Invoke(null, null); } catch (TargetInvocationException tie) { exception = tie.InnerException; } Assert.IsAssignableFrom <ArgumentException>(exception); Assert.Throws <ArgumentException>(() => WriteMethodFactory.CreateWriteMethodForType(type, true)); }
public void Write(Type type, object val, bool isCompileTimeType) { if (type.GetTypeInfo().IsEnum) { type = Enum.GetUnderlyingType(type); } if (type == typeof(bool)) { WriteBoolean((bool)val); return; } else if (type == typeof(byte)) { WriteByte((byte)val); return; } else if (type == typeof(double)) { WriteDouble((double)val); return; } else if (type == typeof(short)) { WriteInt16((short)val); return; } else if (type == typeof(int)) { WriteInt32((int)val); return; } else if (type == typeof(long)) { WriteInt64((long)val); return; } else if (type == typeof(ObjectPath)) { WriteObjectPath((ObjectPath)val); return; } else if (type == typeof(Signature)) { WriteSignature((Signature)val); return; } else if (type == typeof(string)) { WriteString((string)val); return; } else if (type == typeof(float)) { WriteSingle((float)val); return; } else if (type == typeof(ushort)) { WriteUInt16((ushort)val); return; } else if (type == typeof(uint)) { WriteUInt32((uint)val); return; } else if (type == typeof(ulong)) { WriteUInt64((ulong)val); return; } else if (type == typeof(object)) { WriteVariant(val); return; } else if (type == typeof(IDBusObject)) { WriteBusObject((IDBusObject)val); return; } if (ArgTypeInspector.IsDBusObjectType(type, isCompileTimeType)) { WriteBusObject((IDBusObject)val); return; } if (ArgTypeInspector.IsSafeHandleType(type)) { WriteSafeHandle((SafeHandle)val); return; } MethodInfo method = WriteMethodFactory.CreateWriteMethodForType(type, isCompileTimeType); if (method.IsStatic) { method.Invoke(null, new object[] { this, val }); } else { method.Invoke(this, new object[] { val }); } }