2 using System.Collections;
3 using System.Runtime.InteropServices;
4 using System.Reflection.Emit;
8 namespace DBus.DBusType
13 public class Dict : IDBusType
15 public const char Code = 'm';
16 private Hashtable val;
22 public Dict(IDictionary val, Service service)
24 this.val = new Hashtable();
25 foreach (DictionaryEntry entry in val) {
26 this.val.Add(entry.Key, entry.Value);
30 public Dict(IntPtr iter, Service service)
32 IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
34 dbus_message_iter_init_dict_iterator(iter, dictIter);
36 this.val = new Hashtable();
39 string key = dbus_message_iter_get_dict_key(dictIter);
41 // Get the argument type and get the value
42 Type elementType = (Type) DBus.Arguments.DBusTypes[(char) dbus_message_iter_get_arg_type(dictIter)];
43 object [] pars = new Object[1];
45 DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
46 this.val.Add(key, dbusType);
47 } while (dbus_message_iter_next(dictIter));
49 Marshal.FreeCoTaskMem(dictIter);
52 public void Append(IntPtr iter)
54 IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
56 if (!dbus_message_iter_append_dict(iter,
58 throw new ApplicationException("Failed to append DICT argument:" + val);
61 foreach (DictionaryEntry entry in this.val) {
62 if (!dbus_message_iter_append_dict_key(dictIter, (string) entry.Key)) {
63 throw new ApplicationException("Failed to append DICT key:" + entry.Key);
66 // Get the element type
67 Type elementType = Arguments.MatchType(entry.Value.GetType());
68 object [] pars = new Object[1];
69 pars[0] = entry.Value;
70 DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
71 dbusType.Append(dictIter);
74 Marshal.FreeCoTaskMem(dictIter);
77 public static bool Suits(System.Type type)
79 if (typeof(IDictionary).IsAssignableFrom(type)) {
86 public static void EmitMarshalIn(ILGenerator generator, Type type)
89 generator.Emit(OpCodes.Ldind_Ref);
93 public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
95 generator.Emit(OpCodes.Castclass, type);
97 generator.Emit(OpCodes.Stind_Ref);
103 return Get(typeof(Hashtable));
106 public object Get(System.Type type)
111 retVal = (IDictionary) Activator.CreateInstance(type, new object[0]);
112 foreach (DictionaryEntry entry in this.val) {
113 retVal.Add(entry.Key, ((IDBusType) entry.Value).Get());
116 throw new ArgumentException("Cannot cast DBus.Type.Dict to type '" + type.ToString() + "'");
122 [DllImport("dbus-1")]
123 private extern static void dbus_message_iter_init_dict_iterator(IntPtr iter,
126 [DllImport("dbus-1")]
127 private extern static bool dbus_message_iter_append_dict(IntPtr iter,
130 [DllImport("dbus-1")]
131 private extern static bool dbus_message_iter_has_next(IntPtr iter);
133 [DllImport("dbus-1")]
134 private extern static bool dbus_message_iter_next(IntPtr iter);
136 [DllImport("dbus-1")]
137 private extern static string dbus_message_iter_get_dict_key (IntPtr dictIter);
139 [DllImport("dbus-1")]
140 private extern static bool dbus_message_iter_append_dict_key (IntPtr dictIter,
142 [DllImport("dbus-1")]
143 private extern static int dbus_message_iter_get_arg_type(IntPtr iter);