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
13 using System.Globalization;
14 using System.Runtime.CompilerServices;
15 using System.Runtime.InteropServices;
17 namespace System.Reflection {
19 internal struct MonoPropertyInfo {
22 public MethodInfo get_method;
23 public MethodInfo set_method;
24 public PropertyAttributes attrs;
26 [MethodImplAttribute(MethodImplOptions.InternalCall)]
27 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
36 ReflectedType = 1 << 3,
37 DeclaringType = 1 << 4,
41 internal class MonoProperty : PropertyInfo {
42 internal IntPtr klass;
45 public override PropertyAttributes Attributes {
47 MonoPropertyInfo info;
48 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
53 public override bool CanRead {
55 MonoPropertyInfo info;
56 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
57 return (info.get_method != null);
61 public override bool CanWrite {
63 MonoPropertyInfo info;
64 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
65 return (info.set_method != null);
69 public override Type PropertyType {
71 MonoPropertyInfo info;
72 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
74 if (info.get_method != null) {
75 return info.get_method.ReturnType;
77 ParameterInfo[] parameters = info.set_method.GetParameters();
79 return parameters [parameters.Length - 1].ParameterType;
84 public override Type ReflectedType {
86 MonoPropertyInfo info;
87 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
92 public override Type DeclaringType {
94 MonoPropertyInfo info;
95 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
100 public override string Name {
102 MonoPropertyInfo info;
103 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
108 public override MethodInfo[] GetAccessors (bool nonPublic)
110 MonoPropertyInfo info;
114 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
115 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
117 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
120 MethodInfo[] res = new MethodInfo [nget + nset];
123 res [n++] = info.set_method;
125 res [n++] = info.get_method;
129 public override MethodInfo GetGetMethod (bool nonPublic)
131 MonoPropertyInfo info;
132 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
133 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
134 return info.get_method;
139 public override ParameterInfo[] GetIndexParameters()
141 MonoPropertyInfo info;
142 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
143 if (info.get_method != null)
144 return info.get_method.GetParameters ();
145 return new ParameterInfo [0];
148 public override MethodInfo GetSetMethod (bool nonPublic)
150 MonoPropertyInfo info;
151 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
152 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
153 return info.set_method;
158 public override bool IsDefined (Type attributeType, bool inherit)
160 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
163 public override object[] GetCustomAttributes (bool inherit)
165 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
168 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
170 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
173 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
177 MethodInfo method = GetGetMethod (true);
179 throw new ArgumentException ("Get Method not found for '" + Name + "'");
181 if (index == null || index.Length == 0)
182 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
184 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
189 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
191 MethodInfo method = GetSetMethod (true);
193 throw new ArgumentException ("Set Method not found for '" + Name + "'");
196 if (index == null || index.Length == 0)
197 parms = new object [] {value};
199 int ilen = index.Length;
200 parms = new object [ilen+ 1];
201 index.CopyTo (parms, 0);
202 parms [ilen] = value;
205 method.Invoke (obj, invokeAttr, binder, parms, culture);
208 public override string ToString () {
209 return PropertyType.ToString () + " " + Name;