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