2 // System.Reflection/MonoProperty.cs
3 // The class used to represent Properties from the mono runtime.
6 // Paolo Molaro (lupus@ximian.com)
7 // Patrik Torstensson (patrik.torstensson@labs2.com)
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Globalization;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Security;
38 namespace System.Reflection {
40 internal struct MonoPropertyInfo {
43 public MethodInfo get_method;
44 public MethodInfo set_method;
45 public PropertyAttributes attrs;
47 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
51 [MethodImplAttribute (MethodImplOptions.InternalCall)]
52 internal static extern Type[] GetTypeModifiers (MonoProperty prop, bool optional);
61 ReflectedType = 1 << 3,
62 DeclaringType = 1 << 4,
68 internal class MonoProperty : PropertyInfo, ISerializable {
69 internal IntPtr klass;
71 MonoPropertyInfo info;
74 public override PropertyAttributes Attributes {
76 MonoPropertyInfo info;
77 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
82 public override bool CanRead {
84 MonoPropertyInfo info;
85 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
86 return (info.get_method != null);
90 public override bool CanWrite {
92 MonoPropertyInfo info;
93 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
94 return (info.set_method != null);
98 public override Type PropertyType {
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);
105 if (info.get_method != null) {
106 return info.get_method.ReturnType;
108 ParameterInfo[] parameters = info.set_method.GetParameters ();
110 return parameters [parameters.Length - 1].ParameterType;
115 public override Type ReflectedType {
117 MonoPropertyInfo info;
118 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
123 public override Type DeclaringType {
125 if ((cached & PInfo.DeclaringType) == 0) {
126 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
127 cached |= PInfo.DeclaringType;
133 public override string Name {
135 if ((cached & PInfo.Name) == 0) {
136 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
137 cached |= PInfo.Name;
143 public override MethodInfo[] GetAccessors (bool nonPublic)
145 MonoPropertyInfo info;
149 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
150 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
152 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
155 MethodInfo[] res = new MethodInfo [nget + nset];
158 res [n++] = info.set_method;
160 res [n++] = info.get_method;
164 public override MethodInfo GetGetMethod (bool nonPublic)
166 if ((cached & PInfo.GetMethod) == 0) {
167 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
168 cached |= PInfo.GetMethod;
170 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
171 return info.get_method;
176 public override ParameterInfo[] GetIndexParameters()
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];
185 public override MethodInfo GetSetMethod (bool nonPublic)
187 if ((cached & PInfo.SetMethod) == 0) {
188 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
189 cached |= PInfo.SetMethod;
191 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
192 return info.set_method;
197 // According to MSDN the inherit parameter is ignored here and
198 // the behavior always defaults to inherit = false
200 public override bool IsDefined (Type attributeType, bool inherit)
202 return MonoCustomAttrs.IsDefined (this, attributeType, false);
205 public override object[] GetCustomAttributes (bool inherit)
207 return MonoCustomAttrs.GetCustomAttributes (this, false);
210 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
212 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
215 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
219 MethodInfo method = GetGetMethod (true);
221 throw new ArgumentException ("Get Method not found for '" + Name + "'");
224 if (index == null || index.Length == 0)
225 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
227 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
229 catch (SecurityException se) {
230 throw new TargetInvocationException (se);
236 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
238 MethodInfo method = GetSetMethod (true);
240 throw new ArgumentException ("Set Method not found for '" + Name + "'");
243 if (index == null || index.Length == 0)
244 parms = new object [] {value};
246 int ilen = index.Length;
247 parms = new object [ilen+ 1];
248 index.CopyTo (parms, 0);
249 parms [ilen] = value;
252 method.Invoke (obj, invokeAttr, binder, parms, culture);
255 public override string ToString () {
256 return PropertyType.ToString () + " " + Name;
259 #if NET_2_0 || BOOTSTRAP_NET_2_0
261 public override Type[] GetOptionalCustomModifiers () {
262 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
264 return Type.EmptyTypes;
268 public override Type[] GetRequiredCustomModifiers () {
269 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
271 return Type.EmptyTypes;
277 public void GetObjectData (SerializationInfo info, StreamingContext context)
279 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
280 ToString(), MemberTypes.Property);