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 #pragma warning disable 649
70 internal IntPtr klass;
72 MonoPropertyInfo info;
74 #pragma warning restore 649
76 public override PropertyAttributes Attributes {
78 MonoPropertyInfo info;
79 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
84 public override bool CanRead {
86 MonoPropertyInfo info;
87 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
88 return (info.get_method != null);
92 public override bool CanWrite {
94 MonoPropertyInfo info;
95 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
96 return (info.set_method != null);
100 public override Type PropertyType {
102 if ((cached & (PInfo.GetMethod | PInfo.SetMethod)) != (PInfo.GetMethod | PInfo.SetMethod)) {
103 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
104 cached |= (PInfo.GetMethod | PInfo.SetMethod);
107 if (info.get_method != null) {
108 return info.get_method.ReturnType;
110 ParameterInfo[] parameters = info.set_method.GetParameters ();
112 return parameters [parameters.Length - 1].ParameterType;
117 public override Type ReflectedType {
119 MonoPropertyInfo info;
120 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
125 public override Type DeclaringType {
127 if ((cached & PInfo.DeclaringType) == 0) {
128 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
129 cached |= PInfo.DeclaringType;
135 public override string Name {
137 if ((cached & PInfo.Name) == 0) {
138 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
139 cached |= PInfo.Name;
145 public override MethodInfo[] GetAccessors (bool nonPublic)
147 MonoPropertyInfo info;
151 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
152 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
154 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
157 MethodInfo[] res = new MethodInfo [nget + nset];
160 res [n++] = info.set_method;
162 res [n++] = info.get_method;
166 public override MethodInfo GetGetMethod (bool nonPublic)
168 if ((cached & PInfo.GetMethod) == 0) {
169 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
170 cached |= PInfo.GetMethod;
172 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
173 return info.get_method;
178 public override ParameterInfo[] GetIndexParameters()
180 MonoPropertyInfo info;
181 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
182 if (info.get_method != null)
183 return info.get_method.GetParameters ();
184 return new ParameterInfo [0];
187 public override MethodInfo GetSetMethod (bool nonPublic)
189 if ((cached & PInfo.SetMethod) == 0) {
190 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
191 cached |= PInfo.SetMethod;
193 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
194 return info.set_method;
199 // According to MSDN the inherit parameter is ignored here and
200 // the behavior always defaults to inherit = false
202 public override bool IsDefined (Type attributeType, bool inherit)
204 return MonoCustomAttrs.IsDefined (this, attributeType, false);
207 public override object[] GetCustomAttributes (bool inherit)
209 return MonoCustomAttrs.GetCustomAttributes (this, false);
212 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
214 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, false);
217 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
221 MethodInfo method = GetGetMethod (true);
223 throw new ArgumentException ("Get Method not found for '" + Name + "'");
226 if (index == null || index.Length == 0)
227 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
229 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
231 catch (SecurityException se) {
232 throw new TargetInvocationException (se);
238 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
240 MethodInfo method = GetSetMethod (true);
242 throw new ArgumentException ("Set Method not found for '" + Name + "'");
245 if (index == null || index.Length == 0)
246 parms = new object [] {value};
248 int ilen = index.Length;
249 parms = new object [ilen+ 1];
250 index.CopyTo (parms, 0);
251 parms [ilen] = value;
254 method.Invoke (obj, invokeAttr, binder, parms, culture);
257 public override string ToString () {
258 return PropertyType.ToString () + " " + Name;
261 #if NET_2_0 || BOOTSTRAP_NET_2_0
263 public override Type[] GetOptionalCustomModifiers () {
264 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, true);
266 return Type.EmptyTypes;
270 public override Type[] GetRequiredCustomModifiers () {
271 Type[] types = MonoPropertyInfo.GetTypeModifiers (this, false);
273 return Type.EmptyTypes;
279 public void GetObjectData (SerializationInfo info, StreamingContext context)
281 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
282 ToString(), MemberTypes.Property);