00001 namespace DBus
00002 {
00003 using System;
00004 using System.Collections;
00005 using System.Reflection;
00006
00007 internal class InterfaceProxy
00008 {
00009 private static Hashtable interfaceProxies = new Hashtable();
00010 private Hashtable methods = null;
00011 private Hashtable signals = null;
00012
00013 private string interfaceName;
00014
00015 private InterfaceProxy(Type type)
00016 {
00017 object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), true);
00018 InterfaceAttribute interfaceAttribute = (InterfaceAttribute) attributes[0];
00019 this.interfaceName = interfaceAttribute.InterfaceName;
00020 AddMethods(type);
00021 AddSignals(type);
00022 }
00023
00024
00025 private void AddSignals(Type type)
00026 {
00027 this.signals = new Hashtable();
00028 foreach (EventInfo signal in type.GetEvents(BindingFlags.Public |
00029 BindingFlags.Instance |
00030 BindingFlags.DeclaredOnly)) {
00031 object[] attributes = signal.GetCustomAttributes(typeof(SignalAttribute), false);
00032 if (attributes.GetLength(0) > 0) {
00033 MethodInfo invoke = signal.EventHandlerType.GetMethod("Invoke");
00034 signals.Add(signal.Name + " " + GetSignature(invoke), signal);
00035 }
00036 }
00037 }
00038
00039
00040 private void AddMethods(Type type)
00041 {
00042 this.methods = new Hashtable();
00043 foreach (MethodInfo method in type.GetMethods(BindingFlags.Public |
00044 BindingFlags.Instance |
00045 BindingFlags.DeclaredOnly)) {
00046 object[] attributes = method.GetCustomAttributes(typeof(MethodAttribute), false);
00047 if (attributes.GetLength(0) > 0) {
00048 methods.Add(method.Name + " " + GetSignature(method), method);
00049 }
00050 }
00051 }
00052
00053
00054 public static InterfaceProxy GetInterface(Type type)
00055 {
00056 if (!interfaceProxies.Contains(type)) {
00057 interfaceProxies[type] = new InterfaceProxy(type);
00058 }
00059
00060 return (InterfaceProxy) interfaceProxies[type];
00061 }
00062
00063 public bool HasMethod(string key)
00064 {
00065 return this.Methods.Contains(key);
00066 }
00067
00068 public bool HasSignal(string key)
00069 {
00070 return this.Signals.Contains(key);
00071 }
00072
00073 public EventInfo GetSignal(string key)
00074 {
00075 return (EventInfo) this.Signals[key];
00076 }
00077
00078 public MethodInfo GetMethod(string key)
00079 {
00080 return (MethodInfo) this.Methods[key];
00081 }
00082
00083 public static string GetSignature(MethodInfo method)
00084 {
00085 ParameterInfo[] pars = method.GetParameters();
00086 string key = "";
00087
00088 foreach (ParameterInfo par in pars) {
00089 if (!par.IsOut) {
00090 Type dbusType = Arguments.MatchType(par.ParameterType);
00091 key += Arguments.GetCode(dbusType);
00092 }
00093 }
00094
00095 return key;
00096 }
00097
00098 public Hashtable Methods
00099 {
00100 get {
00101 return this.methods;
00102 }
00103 }
00104
00105 public Hashtable Signals
00106 {
00107 get {
00108 return this.signals;
00109 }
00110 }
00111
00112 public string InterfaceName
00113 {
00114 get {
00115 return this.interfaceName;
00116 }
00117 }
00118 }
00119 }
00120
00121