Added InterfaceProxy to Mono bindings to avoid having to generate a proxy for every...
[platform/upstream/dbus.git] / mono / InterfaceProxy.cs
1 namespace DBus
2 {
3   using System;
4   using System.Collections;
5   using System.Reflection;
6   
7   internal class InterfaceProxy
8   {
9     private static Hashtable interfaceProxies = new Hashtable();
10     private Hashtable methods = null;
11
12     private string interfaceName;
13
14     private InterfaceProxy(Type type) 
15     {
16       object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), true);
17       InterfaceAttribute interfaceAttribute = (InterfaceAttribute) attributes[0];
18       this.interfaceName = interfaceAttribute.InterfaceName;
19       AddMethods(type);
20     }
21
22     private void AddMethods(Type type)
23     {
24       this.methods = new Hashtable();
25       foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | 
26                                                     BindingFlags.Instance | 
27                                                     BindingFlags.DeclaredOnly)) {
28         object[] attributes = method.GetCustomAttributes(typeof(MethodAttribute), true);
29         if (attributes.GetLength(0) > 0) {
30           methods.Add(GetKey(method), method);
31         }
32       }
33     }
34     
35
36     public static InterfaceProxy GetInterface(Type type) 
37     {
38       if (!interfaceProxies.Contains(type)) {
39         interfaceProxies[type] = new InterfaceProxy(type);
40       }
41
42       return (InterfaceProxy) interfaceProxies[type];
43     }
44
45     public bool HasMethod(string key) 
46     {
47       return this.Methods.Contains(key);
48     }
49     
50     public MethodInfo GetMethod(string key)
51     {
52       return (MethodInfo) this.Methods[key];
53     }
54
55     private string GetKey(MethodInfo method) 
56     {
57       ParameterInfo[] pars = method.GetParameters();
58       string key = method.Name + " ";
59       
60       foreach (ParameterInfo par in pars) {
61         if (!par.IsOut) {
62           Type dbusType = Arguments.MatchType(par.ParameterType);
63           key += Arguments.GetCode(dbusType);
64         }
65       }
66
67       return key;
68     }
69
70     public Hashtable Methods
71     {
72       get {
73         return this.methods;
74       }
75     }
76     
77     public string InterfaceName
78     {
79       get {
80         return this.interfaceName;
81       }
82     }
83   }
84 }
85
86