Mon Jul 1 18:01:49 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection / MonoProperty.cs
1 //
2 // System.Reflection/MonoProperty.cs
3 // The class used to represent Properties from the mono runtime.
4 //
5 // Author:
6 //   Paolo Molaro (lupus@ximian.com)
7 //   Patrik Torstensson (patrik.torstensson@labs2.com)
8 //
9 // (C) 2001 Ximian, Inc.  http://www.ximian.com
10 //
11
12 using System;
13 using System.Globalization;
14 using System.Runtime.CompilerServices;
15 using System.Runtime.InteropServices;
16
17 namespace System.Reflection {
18         
19         internal struct MonoPropertyInfo {
20                 public Type parent;
21                 public String name;
22                 public MethodInfo get_method;
23                 public MethodInfo set_method;
24                 public PropertyAttributes attrs;
25                 
26                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
27                 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info);
28         }
29
30         internal class MonoProperty : PropertyInfo {
31                 internal IntPtr klass;
32                 internal IntPtr prop;
33                 
34                 public override PropertyAttributes Attributes {
35                         get {
36                                 MonoPropertyInfo info;
37                                 MonoPropertyInfo.get_property_info (this, out info);
38                                 return info.attrs;
39                         }
40                 }
41                 public override bool CanRead {
42                         get {
43                                 MonoPropertyInfo info;
44                                 MonoPropertyInfo.get_property_info (this, out info);
45                                 return (info.get_method != null);
46                         }
47                 }
48                 public override bool CanWrite {
49                         get {
50                                 MonoPropertyInfo info;
51                                 MonoPropertyInfo.get_property_info (this, out info);
52                                 return (info.set_method != null);
53                         }
54                 }
55
56                 public override Type PropertyType {
57                         get {
58                                 MonoPropertyInfo info;
59                                 MonoPropertyInfo.get_property_info (this, out info);
60                                 
61                                 if (info.get_method != null) {
62                                         return info.get_method.ReturnType;
63                                 } else {
64                                         ParameterInfo[] parameters = info.set_method.GetParameters();
65                                         
66                                         return parameters [parameters.Length - 1].ParameterType;
67                                 }
68                         }
69                 }
70                 public override Type ReflectedType {
71                         get {
72                                 MonoPropertyInfo info;
73                                 MonoPropertyInfo.get_property_info (this, out info);
74                                 return info.parent;
75                         }
76                 }
77                 public override Type DeclaringType {
78                         get {
79                                 MonoPropertyInfo info;
80                                 MonoPropertyInfo.get_property_info (this, out info);
81                                 return info.parent;
82                         }
83                 }
84                 public override string Name {
85                         get {
86                                 MonoPropertyInfo info;
87                                 MonoPropertyInfo.get_property_info (this, out info);
88                                 return info.name;
89                         }
90                 }
91
92                 [MonoTODO]
93                 public override MethodInfo[] GetAccessors( bool nonPublic) {
94                         // FIXME: check nonPublic
95                         MonoPropertyInfo info;
96                         int n = 0;
97                         MonoPropertyInfo.get_property_info (this, out info);
98                         if (info.set_method != null)
99                                 n++;
100                         if (info.get_method != null)
101                                 n++;
102                         MethodInfo[] res = new MethodInfo [n];
103                         n = 0;
104                         if (info.set_method != null)
105                                 res [n++] = info.set_method;
106                         if (info.get_method != null)
107                                 res [n++] = info.get_method;
108                         return res;
109                 }
110
111                 [MonoTODO]
112                 public override MethodInfo GetGetMethod( bool nonPublic) {
113                         // FIXME: check nonPublic
114                         MonoPropertyInfo info;
115                         MonoPropertyInfo.get_property_info (this, out info);
116                         return info.get_method;
117                 }
118                 public override ParameterInfo[] GetIndexParameters() {
119                         MonoPropertyInfo info;
120                         MonoPropertyInfo.get_property_info (this, out info);
121                         if (info.get_method != null)
122                                 return info.get_method.GetParameters ();
123                         return new ParameterInfo [0];
124                 }
125                 public override MethodInfo GetSetMethod( bool nonPublic) {
126                         // FIXME: check nonPublic
127                         MonoPropertyInfo info;
128                         MonoPropertyInfo.get_property_info (this, out info);
129                         return info.set_method;
130                 }
131                 public override bool IsDefined (Type attributeType, bool inherit) {
132                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
133                 }
134
135                 public override object[] GetCustomAttributes( bool inherit) {
136                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
137                 }
138                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
139                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
140                 }
141
142                 public override object GetValue( object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) {
143                         object ret = null;
144                         
145                         if (index == null || index.Length == 0) {
146                                 MethodInfo method = GetGetMethod(false);
147
148                                 ret = method.Invoke(obj, invokeAttr, binder, null, culture);
149                         }
150
151                         // fixme: support indexed parameters..
152
153                         return ret;
154                 }
155
156                 public override void SetValue( object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) {
157                 }
158
159                 public override string ToString () {
160                         return PropertyType.ToString () + " " + Name;
161                 }
162         }
163 }
164