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);
109 public override MethodInfo[] GetAccessors (bool nonPublic)
111 // FIXME: check nonPublic
112 MonoPropertyInfo info;
114 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
115 if (info.set_method != null)
117 if (info.get_method != null)
119 MethodInfo[] res = new MethodInfo [n];
121 if (info.set_method != null)
122 res [n++] = info.set_method;
123 if (info.get_method != null)
124 res [n++] = info.get_method;
129 public override MethodInfo GetGetMethod (bool nonPublic)
131 // FIXME: check nonPublic
132 MonoPropertyInfo info;
133 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
134 return info.get_method;
137 public override ParameterInfo[] GetIndexParameters()
139 MonoPropertyInfo info;
140 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
141 if (info.get_method != null)
142 return info.get_method.GetParameters ();
143 return new ParameterInfo [0];
146 public override MethodInfo GetSetMethod (bool nonPublic)
148 // FIXME: check nonPublic
149 MonoPropertyInfo info;
150 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
151 return info.set_method;
154 public override bool IsDefined (Type attributeType, bool inherit)
156 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
159 public override object[] GetCustomAttributes (bool inherit)
161 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
164 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
166 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
169 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
173 MethodInfo method = GetGetMethod (false);
175 throw new ArgumentException ("Get Method not found for '" + Name + "'");
177 if (index == null || index.Length == 0)
178 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
180 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
185 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
187 MethodInfo method = GetSetMethod (false);
189 throw new ArgumentException ("Set Method not found for '" + Name + "'");
192 if (index == null || index.Length == 0)
193 parms = new object [] {value};
195 int ilen = index.Length;
196 parms = new object [ilen+ 1];
197 index.CopyTo (parms, 0);
198 parms [ilen] = value;
201 method.Invoke (obj, invokeAttr, binder, parms, culture);
204 public override string ToString () {
205 return PropertyType.ToString () + " " + Name;