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);
30 internal class MonoProperty : PropertyInfo {
31 internal IntPtr klass;
34 public override PropertyAttributes Attributes {
36 MonoPropertyInfo info;
37 MonoPropertyInfo.get_property_info (this, out info);
42 public override bool CanRead {
44 MonoPropertyInfo info;
45 MonoPropertyInfo.get_property_info (this, out info);
46 return (info.get_method != null);
50 public override bool CanWrite {
52 MonoPropertyInfo info;
53 MonoPropertyInfo.get_property_info (this, out info);
54 return (info.set_method != null);
58 public override Type PropertyType {
60 MonoPropertyInfo info;
61 MonoPropertyInfo.get_property_info (this, out info);
63 if (info.get_method != null) {
64 return info.get_method.ReturnType;
66 ParameterInfo[] parameters = info.set_method.GetParameters();
68 return parameters [parameters.Length - 1].ParameterType;
73 public override Type ReflectedType {
75 MonoPropertyInfo info;
76 MonoPropertyInfo.get_property_info (this, out info);
81 public override Type DeclaringType {
83 MonoPropertyInfo info;
84 MonoPropertyInfo.get_property_info (this, out info);
89 public override string Name {
91 MonoPropertyInfo info;
92 MonoPropertyInfo.get_property_info (this, out info);
98 public override MethodInfo[] GetAccessors (bool nonPublic)
100 // FIXME: check nonPublic
101 MonoPropertyInfo info;
103 MonoPropertyInfo.get_property_info (this, out info);
104 if (info.set_method != null)
106 if (info.get_method != null)
108 MethodInfo[] res = new MethodInfo [n];
110 if (info.set_method != null)
111 res [n++] = info.set_method;
112 if (info.get_method != null)
113 res [n++] = info.get_method;
118 public override MethodInfo GetGetMethod (bool nonPublic)
120 // FIXME: check nonPublic
121 MonoPropertyInfo info;
122 MonoPropertyInfo.get_property_info (this, out info);
123 return info.get_method;
126 public override ParameterInfo[] GetIndexParameters()
128 MonoPropertyInfo info;
129 MonoPropertyInfo.get_property_info (this, out info);
130 if (info.get_method != null)
131 return info.get_method.GetParameters ();
132 return new ParameterInfo [0];
135 public override MethodInfo GetSetMethod (bool nonPublic)
137 // FIXME: check nonPublic
138 MonoPropertyInfo info;
139 MonoPropertyInfo.get_property_info (this, out info);
140 return info.set_method;
143 public override bool IsDefined (Type attributeType, bool inherit)
145 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
148 public override object[] GetCustomAttributes (bool inherit)
150 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
153 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
155 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
158 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
162 MethodInfo method = GetGetMethod (false);
164 throw new ArgumentException ("Get Method not found for '" + Name + "'");
166 if (index == null || index.Length == 0)
167 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
169 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
174 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
176 MethodInfo method = GetSetMethod (false);
178 throw new ArgumentException ("Set Method not found for '" + Name + "'");
181 if (index == null || index.Length == 0)
182 parms = new object [] {value};
184 int ilen = index.Length;
185 parms = new object [ilen+ 1];
186 index.CopyTo (parms, 0);
187 parms [ilen] = value;
190 method.Invoke (obj, invokeAttr, binder, parms, culture);
193 public override string ToString () {
194 return PropertyType.ToString () + " " + Name;