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.Security;
37 namespace System.Reflection {
39 internal struct MonoPropertyInfo {
42 public MethodInfo get_method;
43 public MethodInfo set_method;
44 public PropertyAttributes attrs;
46 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
56 ReflectedType = 1 << 3,
57 DeclaringType = 1 << 4,
61 internal class MonoProperty : PropertyInfo {
62 internal IntPtr klass;
65 public override PropertyAttributes Attributes {
67 MonoPropertyInfo info;
68 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
73 public override bool CanRead {
75 MonoPropertyInfo info;
76 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
77 return (info.get_method != null);
81 public override bool CanWrite {
83 MonoPropertyInfo info;
84 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
85 return (info.set_method != null);
89 public override Type PropertyType {
91 MonoPropertyInfo info;
92 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
94 if (info.get_method != null) {
95 return info.get_method.ReturnType;
97 ParameterInfo[] parameters = info.set_method.GetParameters();
99 return parameters [parameters.Length - 1].ParameterType;
104 public override Type ReflectedType {
106 MonoPropertyInfo info;
107 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
112 public override Type DeclaringType {
114 MonoPropertyInfo info;
115 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
120 public override string Name {
122 MonoPropertyInfo info;
123 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
128 public override MethodInfo[] GetAccessors (bool nonPublic)
130 MonoPropertyInfo info;
134 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
135 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
137 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
140 MethodInfo[] res = new MethodInfo [nget + nset];
143 res [n++] = info.set_method;
145 res [n++] = info.get_method;
149 public override MethodInfo GetGetMethod (bool nonPublic)
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;
159 public override ParameterInfo[] GetIndexParameters()
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];
168 public override MethodInfo GetSetMethod (bool nonPublic)
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;
178 public override bool IsDefined (Type attributeType, bool inherit)
180 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
183 public override object[] GetCustomAttributes (bool inherit)
185 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
188 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
190 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
193 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
197 MethodInfo method = GetGetMethod (true);
199 throw new ArgumentException ("Get Method not found for '" + Name + "'");
202 if (index == null || index.Length == 0)
203 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
205 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
207 catch (SecurityException se) {
208 throw new TargetInvocationException (se);
214 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
216 MethodInfo method = GetSetMethod (true);
218 throw new ArgumentException ("Set Method not found for '" + Name + "'");
221 if (index == null || index.Length == 0)
222 parms = new object [] {value};
224 int ilen = index.Length;
225 parms = new object [ilen+ 1];
226 index.CopyTo (parms, 0);
227 parms [ilen] = value;
230 method.Invoke (obj, invokeAttr, binder, parms, culture);
233 public override string ToString () {
234 return PropertyType.ToString () + " " + Name;