New test.
[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.Security;
36
37 namespace System.Reflection {
38         
39         internal struct MonoPropertyInfo {
40                 public Type parent;
41                 public String name;
42                 public MethodInfo get_method;
43                 public MethodInfo set_method;
44                 public PropertyAttributes attrs;
45                 
46                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47                 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
48                                                                PInfo req_info);
49         }
50
51         [Flags]
52         internal enum PInfo {
53                 Attributes = 1,
54                 GetMethod  = 1 << 1,
55                 SetMethod  = 1 << 2,
56                 ReflectedType = 1 << 3,
57                 DeclaringType = 1 << 4,
58                 Name = 1 << 5
59                 
60         }
61         internal class MonoProperty : PropertyInfo {
62                 internal IntPtr klass;
63                 internal IntPtr prop;
64                 
65                 public override PropertyAttributes Attributes {
66                         get {
67                                 MonoPropertyInfo info;
68                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
69                                 return info.attrs;
70                         }
71                 }
72                 
73                 public override bool CanRead {
74                         get {
75                                 MonoPropertyInfo info;
76                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
77                                 return (info.get_method != null);
78                         }
79                 }
80                 
81                 public override bool CanWrite {
82                         get {
83                                 MonoPropertyInfo info;
84                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
85                                 return (info.set_method != null);
86                         }
87                 }
88
89                 public override Type PropertyType {
90                         get {
91                                 MonoPropertyInfo info;
92                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
93                                 
94                                 if (info.get_method != null) {
95                                         return info.get_method.ReturnType;
96                                 } else {
97                                         ParameterInfo[] parameters = info.set_method.GetParameters();
98                                         
99                                         return parameters [parameters.Length - 1].ParameterType;
100                                 }
101                         }
102                 }
103
104                 public override Type ReflectedType {
105                         get {
106                                 MonoPropertyInfo info;
107                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
108                                 return info.parent;
109                         }
110                 }
111                 
112                 public override Type DeclaringType {
113                         get {
114                                 MonoPropertyInfo info;
115                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
116                                 return info.parent;
117                         }
118                 }
119                 
120                 public override string Name {
121                         get {
122                                 MonoPropertyInfo info;
123                                 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
124                                 return info.name;
125                         }
126                 }
127
128                 public override MethodInfo[] GetAccessors (bool nonPublic)
129                 {
130                         MonoPropertyInfo info;
131                         int nget = 0;
132                         int nset = 0;
133                         
134                         MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
135                         if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
136                                 nset = 1;
137                         if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
138                                 nget = 1;
139
140                         MethodInfo[] res = new MethodInfo [nget + nset];
141                         int n = 0;
142                         if (nset != 0)
143                                 res [n++] = info.set_method;
144                         if (nget != 0)
145                                 res [n++] = info.get_method;
146                         return res;
147                 }
148
149                 public override MethodInfo GetGetMethod (bool nonPublic)
150                 {
151                         MonoPropertyInfo info;
152                         MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
153                         if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
154                                 return info.get_method;
155                         else
156                                 return null;
157                 }
158
159                 public override ParameterInfo[] GetIndexParameters()
160                 {
161                         MonoPropertyInfo info;
162                         MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
163                         if (info.get_method != null)
164                                 return info.get_method.GetParameters ();
165                         return new ParameterInfo [0];
166                 }
167                 
168                 public override MethodInfo GetSetMethod (bool nonPublic)
169                 {
170                         MonoPropertyInfo info;
171                         MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
172                         if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
173                                 return info.set_method;
174                         else
175                                 return null;
176                 }
177                 
178                 public override bool IsDefined (Type attributeType, bool inherit)
179                 {
180                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
181                 }
182
183                 public override object[] GetCustomAttributes (bool inherit)
184                 {
185                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
186                 }
187                 
188                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
189                 {
190                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
191                 }
192
193                 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
194                 {
195                         object ret = null;
196
197                         MethodInfo method = GetGetMethod (true);
198                         if (method == null)
199                                 throw new ArgumentException ("Get Method not found for '" + Name + "'");
200
201                         try {
202                                 if (index == null || index.Length == 0) 
203                                         ret = method.Invoke (obj, invokeAttr, binder, null, culture);
204                                 else
205                                         ret = method.Invoke (obj, invokeAttr, binder, index, culture);
206                         }
207                         catch (SecurityException se) {
208                                 throw new TargetInvocationException (se);
209                         }
210
211                         return ret;
212                 }
213
214                 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
215                 {
216                         MethodInfo method = GetSetMethod (true);
217                         if (method == null)
218                                 throw new ArgumentException ("Set Method not found for '" + Name + "'");
219                         
220                         object [] parms;
221                         if (index == null || index.Length == 0) 
222                                 parms = new object [] {value};
223                         else {
224                                 int ilen = index.Length;
225                                 parms = new object [ilen+ 1];
226                                 index.CopyTo (parms, 0);
227                                 parms [ilen] = value;
228                         }
229
230                         method.Invoke (obj, invokeAttr, binder, parms, culture);
231                 }
232
233                 public override string ToString () {
234                         return PropertyType.ToString () + " " + Name;
235                 }
236         }
237 }
238