Added InterfaceProxy to Mono bindings to avoid having to generate a proxy for every...
[platform/upstream/dbus.git] / mono / Introspector.cs
1 namespace DBus 
2 {
3   
4   using System;
5   using System.Runtime.InteropServices;
6   using System.Diagnostics;
7   using System.Collections;
8   using System.Reflection;
9   
10   internal class Introspector
11   {
12     private Type type;
13     private static Hashtable introspectors = new Hashtable();
14     private Hashtable interfaceProxies = null;
15     
16     public static Introspector GetIntrospector(Type type) 
17     {
18       if (!introspectors.Contains(type)) {
19         introspectors[type] = new Introspector(type);
20       }
21
22       return (Introspector) introspectors[type];
23     }
24
25     private Introspector(Type type) 
26     {
27       interfaceProxies = new Hashtable();
28       AddType(type);
29       this.type = type;
30     }
31     
32     private void AddType(Type type) 
33     {
34       if (type == typeof(object)) {
35         // Base case
36         return;
37       }
38
39       object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), false);
40       if (attributes.Length >= 1) {
41         // This is a D-BUS interface so add it to the hashtable
42         InterfaceProxy interfaceProxy = InterfaceProxy.GetInterface(type);
43         interfaceProxies.Add(interfaceProxy.InterfaceName, interfaceProxy);
44       }
45
46       AddType(type.BaseType);
47     }
48     
49     public InterfaceProxy GetInterface(string interfaceName) {
50       if (interfaceProxies.Contains(interfaceName)) {
51         return (InterfaceProxy) interfaceProxies[interfaceName];
52       } else {
53         return null;
54       }
55     }
56
57     public Hashtable InterfaceProxies
58     {
59       get {
60         return this.interfaceProxies;
61       }
62     }
63
64     public ConstructorInfo Constructor
65     {
66       get {
67         ConstructorInfo ret = this.type.GetConstructor(new Type[0]);
68         if (ret != null) {
69           return ret;
70         } else {
71           return typeof(object).GetConstructor(new Type[0]);
72         }
73       }
74     }
75
76     public override string ToString()
77     {
78       return this.type.ToString();
79     }
80   }
81 }