summaryrefslogtreecommitdiffstats
path: root/mono/InterfaceProxy.cs
blob: 50697738b8339b853ad7b358ec510c7fa1e64d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
namespace DBus
{
  using System;
  using System.Collections;
  using System.Reflection;
  
  internal class InterfaceProxy
  {
    private static Hashtable interfaceProxies = new Hashtable();
    private Hashtable methods = null;
    private Hashtable signals = null;
    
    private string interfaceName;

    private InterfaceProxy(Type type) 
    {
      object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), true);
      InterfaceAttribute interfaceAttribute = (InterfaceAttribute) attributes[0];
      this.interfaceName = interfaceAttribute.InterfaceName;
      AddMethods(type);
      AddSignals(type);
    }

    // Add all the events with Signal attributes
    private void AddSignals(Type type)
    {
      this.signals = new Hashtable();
      foreach (EventInfo signal in type.GetEvents(BindingFlags.Public |
						  BindingFlags.Instance |
						  BindingFlags.DeclaredOnly)) {
	object[] attributes = signal.GetCustomAttributes(typeof(SignalAttribute), false);
	if (attributes.GetLength(0) > 0) {
	  MethodInfo invoke = signal.EventHandlerType.GetMethod("Invoke");
	  signals.Add(signal.Name + " " + GetSignature(invoke), signal);
	}
      }      
    }

    // Add all the methods with Method attributes
    private void AddMethods(Type type)
    {
      this.methods = new Hashtable();
      foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | 
						    BindingFlags.Instance | 
						    BindingFlags.DeclaredOnly)) {
	object[] attributes = method.GetCustomAttributes(typeof(MethodAttribute), false);
	if (attributes.GetLength(0) > 0) {
	  methods.Add(method.Name + " " + GetSignature(method), method);
	}
      }
    }
    

    public static InterfaceProxy GetInterface(Type type) 
    {
      if (!interfaceProxies.Contains(type)) {
	interfaceProxies[type] = new InterfaceProxy(type);
      }

      return (InterfaceProxy) interfaceProxies[type];
    }

    public bool HasMethod(string key) 
    {
      return this.Methods.Contains(key);
    }

    public bool HasSignal(string key)
    {
      return this.Signals.Contains(key);
    }
    
    public EventInfo GetSignal(string key)
    {
      return (EventInfo) this.Signals[key];
    }
    
    public MethodInfo GetMethod(string key)
    {
      return (MethodInfo) this.Methods[key];
    }

    public static string GetSignature(MethodInfo method) 
    {
      ParameterInfo[] pars = method.GetParameters();
      string key = "";
      
      foreach (ParameterInfo par in pars) {
	if (!par.IsOut) {
	  Type dbusType = Arguments.MatchType(par.ParameterType);
	  key += Arguments.GetCode(dbusType);
	}
      }

      return key;
    }

    public Hashtable Methods
    {
      get {
	return this.methods;
      }
    }

    public Hashtable Signals
    {
      get {
	return this.signals;
      }
    }
    
    public string InterfaceName
    {
      get {
	return this.interfaceName;
      }
    }
  }
}