This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Globalization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39
40 namespace System.Reflection {
41         
42         internal struct MonoPropertyInfo {
43                 public Type parent;
44                 public String name;
45                 public MethodInfo get_method;
46                 public MethodInfo set_method;
47                 public PropertyAttributes attrs;
48                 
49                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
50                 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
51                                                                PInfo req_info);
52         }
53
54         [Flags]
55         internal enum PInfo {
56                 Attributes = 1,
57                 GetMethod  = 1 << 1,
58                 SetMethod  = 1 << 2,
59                 ReflectedType = 1 << 3,
60                 DeclaringType = 1 << 4,
61                 Name = 1 << 5
62                 
63         }
64         internal class MonoProperty : PropertyInfo {
65                 internal IntPtr klass;
66                 internal IntPtr prop;
67                 
68                 public override PropertyAttributes Attributes {
69                         get {
70                                 MonoPropertyInfo info;
71                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
72                                 return info.attrs;
73                         }
74                 }
75                 
76                 public override bool CanRead {
77                         get {
78                                 MonoPropertyInfo info;
79                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
80                                 return (info.get_method != null);
81                         }
82                 }
83                 
84                 public override bool CanWrite {
85                         get {
86                                 MonoPropertyInfo info;
87                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
88                                 return (info.set_method != null);
89                         }
90                 }
91
92                 public override Type PropertyType {
93                         get {
94                                 MonoPropertyInfo info;
95                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
96                                 
97                                 if (info.get_method != null) {
98                                         return info.get_method.ReturnType;
99                                 } else {
100                                         ParameterInfo[] parameters = info.set_method.GetParameters();
101                                         
102                                         return parameters [parameters.Length - 1].ParameterType;
103                                 }
104                         }
105                 }
106
107                 public override Type ReflectedType {
108                         get {
109                                 MonoPropertyInfo info;
110                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
111                                 return info.parent;
112                         }
113                 }
114                 
115                 public override Type DeclaringType {
116                         get {
117                                 MonoPropertyInfo info;
118                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
119                                 return info.parent;
120                         }
121                 }
122                 
123                 public override string Name {
124                         get {
125                                 MonoPropertyInfo info;
126                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
127                                 return info.name;
128                         }
129                 }
130
131                 public override MethodInfo[] GetAccessors (bool nonPublic)
132                 {
133                         MonoPropertyInfo info;
134                         int nget = 0;
135                         int nset = 0;
136                         
137                         MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
138                         if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
139                                 nset = 1;
140                         if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
141                                 nget = 1;
142
143                         MethodInfo[] res = new MethodInfo [nget + nset];
144                         int n = 0;
145                         if (nset != 0)
146                                 res [n++] = info.set_method;
147                         if (nget != 0)
148                                 res [n++] = info.get_method;
149                         return res;
150                 }
151
152                 public override MethodInfo GetGetMethod (bool nonPublic)
153                 {
154                         MonoPropertyInfo info;
155                         MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
156                         if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
157                                 return info.get_method;
158                         else
159                                 return null;
160                 }
161
162                 public override ParameterInfo[] GetIndexParameters()
163                 {
164                         MonoPropertyInfo info;
165                         MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
166                         if (info.get_method != null)
167                                 return info.get_method.GetParameters ();
168                         return new ParameterInfo [0];
169                 }
170                 
171                 public override MethodInfo GetSetMethod (bool nonPublic)
172                 {
173                         MonoPropertyInfo info;
174                         MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
175                         if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
176                                 return info.set_method;
177                         else
178                                 return null;
179                 }
180                 
181                 public override bool IsDefined (Type attributeType, bool inherit)
182                 {
183                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
184                 }
185
186                 public override object[] GetCustomAttributes (bool inherit)
187                 {
188                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
189                 }
190                 
191                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
192                 {
193                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
194                 }
195
196                 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
197                 {
198                         object ret = null;
199
200                         MethodInfo method = GetGetMethod (true);
201                         if (method == null)
202                                 throw new ArgumentException ("Get Method not found for '" + Name + "'");
203                         
204                         if (index == null || index.Length == 0) 
205                                 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
206                         else
207                                 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
208
209                         return ret;
210                 }
211
212                 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
213                 {
214                         MethodInfo method = GetSetMethod (true);
215                         if (method == null)
216                                 throw new ArgumentException ("Set Method not found for '" + Name + "'");
217                         
218                         object [] parms;
219                         if (index == null || index.Length == 0) 
220                                 parms = new object [] {value};
221                         else {
222                                 int ilen = index.Length;
223                                 parms = new object [ilen+ 1];
224                                 index.CopyTo (parms, 0);
225                                 parms [ilen] = value;
226                         }
227
228                         method.Invoke (obj, invokeAttr, binder, parms, culture);
229                 }
230
231                 public override string ToString () {
232                         return PropertyType.ToString () + " " + Name;
233                 }
234         }
235 }
236