2 using System.Runtime.InteropServices;
3 using System.Reflection.Emit;
7 namespace DBus.DBusType
10 /// A named byte array, used for custom types.
12 public class Custom : IDBusType
14 public const char Code = 'c';
15 private DBus.Custom val;
21 public Custom(DBus.Custom val, Service service)
26 public Custom(IntPtr iter, Service service)
32 if (!dbus_message_iter_get_custom(iter, out name, out value, out len)) {
33 throw new ApplicationException("Failed to get CUSTOM argument.");
37 this.val.Data = new byte[len];
38 Marshal.Copy(value, this.val.Data, 0, len);
41 public void Append(IntPtr iter)
43 IntPtr data = Marshal.AllocCoTaskMem(this.val.Data.Length);
45 Marshal.Copy(this.val.Data, 0, data, this.val.Data.Length);
46 if (!dbus_message_iter_append_custom(iter, this.val.Name, data, this.val.Data.Length)) {
47 throw new ApplicationException("Failed to append CUSTOM argument:" + val);
50 Marshal.FreeCoTaskMem(data);
54 public static bool Suits(System.Type type)
56 switch (type.ToString()) {
65 public static void EmitMarshalIn(ILGenerator generator, Type type)
68 generator.Emit(OpCodes.Ldobj, type);
72 public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
74 generator.Emit(OpCodes.Unbox, type);
75 generator.Emit(OpCodes.Ldobj, type);
77 generator.Emit(OpCodes.Stobj, type);
86 public object Get(System.Type type)
88 switch (type.ToString()) {
93 throw new ArgumentException("Cannot cast DBus.Type.Custom to type '" + type.ToString() + "'");
98 private extern static bool dbus_message_iter_get_custom(IntPtr iter,
103 [DllImport("dbus-1")]
104 private extern static bool dbus_message_iter_append_custom(IntPtr iter,