00001 using System; 00002 using System.Runtime.InteropServices; 00003 using System.Reflection.Emit; 00004 00005 using DBus; 00006 00007 namespace DBus.DBusType 00008 { 00012 public class Byte : IDBusType 00013 { 00014 public const char Code = 'y'; 00015 private System.Byte val; 00016 00017 private Byte() 00018 { 00019 } 00020 00021 public Byte(System.Byte val, Service service) 00022 { 00023 this.val = val; 00024 } 00025 00026 public Byte(System.Char val, Service service) 00027 { 00028 this.val = (byte) val; 00029 } 00030 00031 public Byte(IntPtr iter, Service service) 00032 { 00033 dbus_message_iter_get_basic (iter, out this.val); 00034 } 00035 00036 public void Append(IntPtr iter) 00037 { 00038 if (!dbus_message_iter_append_basic (iter, (int) Code, ref val)) 00039 throw new ApplicationException("Failed to append BYTE argument:" + val); 00040 } 00041 00042 public static bool Suits(System.Type type) 00043 { 00044 if (type.IsEnum && Enum.GetUnderlyingType (type) == typeof(System.Byte)) { 00045 return true; 00046 } 00047 00048 switch (type.ToString()) { 00049 case "System.Byte": 00050 case "System.Byte&": 00051 case "System.Char": 00052 case "System.Char&": 00053 return true; 00054 } 00055 00056 return false; 00057 } 00058 00059 public static void EmitMarshalIn(ILGenerator generator, Type type) 00060 { 00061 if (type.IsByRef) { 00062 generator.Emit(OpCodes.Ldind_U1); 00063 } 00064 } 00065 00066 public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 00067 { 00068 generator.Emit(OpCodes.Unbox, type); 00069 generator.Emit(OpCodes.Ldind_U1); 00070 if (!isReturn) { 00071 generator.Emit(OpCodes.Stind_I1); 00072 } 00073 } 00074 00075 public object Get() 00076 { 00077 return this.val; 00078 } 00079 00080 public object Get(System.Type type) 00081 { 00082 if (type.IsEnum) { 00083 return Enum.ToObject(type, this.val); 00084 } 00085 00086 switch (type.ToString()) { 00087 case "System.Byte": 00088 case "System.Byte&": 00089 return this.val; 00090 case "System.Char": 00091 case "System.Char&": 00092 char charVal = (char) this.val; 00093 return charVal; 00094 default: 00095 throw new ArgumentException("Cannot cast DBus.Type.Byte to type '" + type.ToString() + "'"); 00096 } 00097 } 00098 00099 [DllImport("dbus-1")] 00100 private extern static void dbus_message_iter_get_basic (IntPtr iter, out byte value); 00101 00102 [DllImport("dbus-1")] 00103 private extern static bool dbus_message_iter_append_basic (IntPtr iter, int type, ref byte value); 00104 } 00105 }