summaryrefslogtreecommitdiffstats
path: root/mono/Introspector.cs
blob: c7b9d05b4d74bd66671d3a95dbfb0d820efe4b97 (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
namespace DBus 
{
  
  using System;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  using System.Collections;
  using System.Reflection;
  
  internal class Introspector
  {
    private Type type;
    private string interfaceName;
    
    public Introspector(Type type)    {
      object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), true);
      if (attributes.Length != 1)
	throw new ApplicationException("Type '" + type + "' is not a D-BUS interface.");
      
      InterfaceAttribute interfaceAttribute = (InterfaceAttribute) attributes[0];
     
      this.interfaceName = interfaceAttribute.InterfaceName;
      this.type = type;
    }
    
    public string InterfaceName
    {
      get
	{
	  return this.interfaceName;
	}
    }

    public ConstructorInfo Constructor
    {
      get
	{
	  ConstructorInfo ret = this.type.GetConstructor(new Type[0]);
	  if (ret != null) {
	    return ret;
	  } else {
	    return typeof(object).GetConstructor(new Type[0]);
	  }
	}
    }

    public IntrospectorMethods Methods
    {
      get
	{
	  return new IntrospectorMethods(this.type);
	}
    }

    public class IntrospectorMethods : IEnumerable
    {
      private Type type;
      
      public IntrospectorMethods(Type type)
      {
	this.type = type;
      }

      public IEnumerator GetEnumerator()
      {
	return new MethodEnumerator(this.type.GetMethods(BindingFlags.Public|BindingFlags.Instance).GetEnumerator());
      }

      private class MethodEnumerator : IEnumerator
      {
	private IEnumerator enumerator;
	
	public MethodEnumerator(IEnumerator enumerator)
	{
	  this.enumerator = enumerator;
	}
	
	public bool MoveNext()
	{
	  while (enumerator.MoveNext()) {
	    MethodInfo method = (MethodInfo) enumerator.Current;
	    object[] attributes = method.GetCustomAttributes(typeof(MethodAttribute), true);
	    if (attributes.GetLength(0) > 0) {
	      return true;
	    }
	  }
	  
	  return false;
	}
	
	public void Reset()
	{
	  enumerator.Reset();
	}
	
	public object Current
	{
	  get
	    {
	      return enumerator.Current;
	    }
	}
      }
    }
  }
}