* qt/Makfile.am:
[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     private Hashtable signals = null;
12     
13     private string interfaceName;
14
15     private InterfaceProxy(Type type) 
16     {
17       object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), true);
18       InterfaceAttribute interfaceAttribute = (InterfaceAttribute) attributes[0];
19       this.interfaceName = interfaceAttribute.InterfaceName;
20       AddMethods(type);
21       AddSignals(type);
22     }
23
24     // Add all the events with Signal attributes
25     private void AddSignals(Type type)
26     {
27       this.signals = new Hashtable();
28       foreach (EventInfo signal in type.GetEvents(BindingFlags.Public |
29                                                   BindingFlags.Instance |
30                                                   BindingFlags.DeclaredOnly)) {
31         object[] attributes = signal.GetCustomAttributes(typeof(SignalAttribute), false);
32         if (attributes.GetLength(0) > 0) {
33           MethodInfo invoke = signal.EventHandlerType.GetMethod("Invoke");
34           signals.Add(signal.Name + " " + GetSignature(invoke), signal);
35         }
36       }      
37     }
38
39     // Add all the methods with Method attributes
40     private void AddMethods(Type type)
41     {
42       this.methods = new Hashtable();
43       foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | 
44                                                     BindingFlags.Instance | 
45                                                     BindingFlags.DeclaredOnly)) {
46         object[] attributes = method.GetCustomAttributes(typeof(MethodAttribute), false);
47         if (attributes.GetLength(0) > 0) {
48           methods.Add(method.Name + " " + GetSignature(method), method);
49         }
50       }
51     }
52     
53
54     public static InterfaceProxy GetInterface(Type type) 
55     {
56       if (!interfaceProxies.Contains(type)) {
57         interfaceProxies[type] = new InterfaceProxy(type);
58       }
59
60       return (InterfaceProxy) interfaceProxies[type];
61     }
62
63     public bool HasMethod(string key) 
64     {
65       return this.Methods.Contains(key);
66     }
67
68     public bool HasSignal(string key)
69     {
70       return this.Signals.Contains(key);
71     }
72     
73     public EventInfo GetSignal(string key)
74     {
75       return (EventInfo) this.Signals[key];
76     }
77     
78     public MethodInfo GetMethod(string key)
79     {
80       return (MethodInfo) this.Methods[key];
81     }
82
83     public static string GetSignature(MethodInfo method) 
84     {
85       ParameterInfo[] pars = method.GetParameters();
86       string key = "";
87       
88       foreach (ParameterInfo par in pars) {
89         if (!par.IsOut) {
90           Type dbusType = Arguments.MatchType(par.ParameterType);
91           key += Arguments.GetCode(dbusType);
92         }
93       }
94
95       return key;
96     }
97
98     public Hashtable Methods
99     {
100       get {
101         return this.methods;
102       }
103     }
104
105     public Hashtable Signals
106     {
107       get {
108         return this.signals;
109       }
110     }
111     
112     public string InterfaceName
113     {
114       get {
115         return this.interfaceName;
116       }
117     }
118   }
119 }
120
121