(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Reflection / MonoProperty.cs
blob901a83fb7e3855edc13414362049c4521bc3e4cb
1 //
2 // System.Reflection/MonoProperty.cs
3 // The class used to represent Properties from the mono runtime.
4 //
5 // Author:
6 // Paolo Molaro (lupus@ximian.com)
7 // Patrik Torstensson (patrik.torstensson@labs2.com)
8 //
9 // (C) 2001 Ximian, Inc. http://www.ximian.com
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System;
36 using System.Globalization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
40 namespace System.Reflection {
42 internal struct MonoPropertyInfo {
43 public Type parent;
44 public String name;
45 public MethodInfo get_method;
46 public MethodInfo set_method;
47 public PropertyAttributes attrs;
49 [MethodImplAttribute(MethodImplOptions.InternalCall)]
50 internal static extern void get_property_info (MonoProperty prop, out MonoPropertyInfo info,
51 PInfo req_info);
54 [Flags]
55 internal enum PInfo {
56 Attributes = 1,
57 GetMethod = 1 << 1,
58 SetMethod = 1 << 2,
59 ReflectedType = 1 << 3,
60 DeclaringType = 1 << 4,
61 Name = 1 << 5
64 internal class MonoProperty : PropertyInfo {
65 internal IntPtr klass;
66 internal IntPtr prop;
68 public override PropertyAttributes Attributes {
69 get {
70 MonoPropertyInfo info;
71 MonoPropertyInfo.get_property_info (this, out info, PInfo.Attributes);
72 return info.attrs;
76 public override bool CanRead {
77 get {
78 MonoPropertyInfo info;
79 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
80 return (info.get_method != null);
84 public override bool CanWrite {
85 get {
86 MonoPropertyInfo info;
87 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
88 return (info.set_method != null);
92 public override Type PropertyType {
93 get {
94 MonoPropertyInfo info;
95 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
97 if (info.get_method != null) {
98 return info.get_method.ReturnType;
99 } else {
100 ParameterInfo[] parameters = info.set_method.GetParameters();
102 return parameters [parameters.Length - 1].ParameterType;
107 public override Type ReflectedType {
108 get {
109 MonoPropertyInfo info;
110 MonoPropertyInfo.get_property_info (this, out info, PInfo.ReflectedType);
111 return info.parent;
115 public override Type DeclaringType {
116 get {
117 MonoPropertyInfo info;
118 MonoPropertyInfo.get_property_info (this, out info, PInfo.DeclaringType);
119 return info.parent;
123 public override string Name {
124 get {
125 MonoPropertyInfo info;
126 MonoPropertyInfo.get_property_info (this, out info, PInfo.Name);
127 return info.name;
131 public override MethodInfo[] GetAccessors (bool nonPublic)
133 MonoPropertyInfo info;
134 int nget = 0;
135 int nset = 0;
137 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod | PInfo.SetMethod);
138 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
139 nset = 1;
140 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
141 nget = 1;
143 MethodInfo[] res = new MethodInfo [nget + nset];
144 int n = 0;
145 if (nset != 0)
146 res [n++] = info.set_method;
147 if (nget != 0)
148 res [n++] = info.get_method;
149 return res;
152 public override MethodInfo GetGetMethod (bool nonPublic)
154 MonoPropertyInfo info;
155 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
156 if (info.get_method != null && (nonPublic || info.get_method.IsPublic))
157 return info.get_method;
158 else
159 return null;
162 public override ParameterInfo[] GetIndexParameters()
164 MonoPropertyInfo info;
165 MonoPropertyInfo.get_property_info (this, out info, PInfo.GetMethod);
166 if (info.get_method != null)
167 return info.get_method.GetParameters ();
168 return new ParameterInfo [0];
171 public override MethodInfo GetSetMethod (bool nonPublic)
173 MonoPropertyInfo info;
174 MonoPropertyInfo.get_property_info (this, out info, PInfo.SetMethod);
175 if (info.set_method != null && (nonPublic || info.set_method.IsPublic))
176 return info.set_method;
177 else
178 return null;
181 public override bool IsDefined (Type attributeType, bool inherit)
183 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
186 public override object[] GetCustomAttributes (bool inherit)
188 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
191 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
193 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
196 public override object GetValue (object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
198 object ret = null;
200 MethodInfo method = GetGetMethod (true);
201 if (method == null)
202 throw new ArgumentException ("Get Method not found for '" + Name + "'");
204 if (index == null || index.Length == 0)
205 ret = method.Invoke (obj, invokeAttr, binder, null, culture);
206 else
207 ret = method.Invoke (obj, invokeAttr, binder, index, culture);
209 return ret;
212 public override void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
214 MethodInfo method = GetSetMethod (true);
215 if (method == null)
216 throw new ArgumentException ("Set Method not found for '" + Name + "'");
218 object [] parms;
219 if (index == null || index.Length == 0)
220 parms = new object [] {value};
221 else {
222 int ilen = index.Length;
223 parms = new object [ilen+ 1];
224 index.CopyTo (parms, 0);
225 parms [ilen] = value;
228 method.Invoke (obj, invokeAttr, binder, parms, culture);
231 public override string ToString () {
232 return PropertyType.ToString () + " " + Name;
235 #if NET_2_0 || BOOTSTRAP_NET_2_0
236 [MonoTODO]
237 public override Type[] OptionalCustomModifiers {
238 get {
239 throw new NotImplementedException ();
243 [MonoTODO]
244 public override Type[] RequiredCustomModifiers {
245 get {
246 throw new NotImplementedException ();
249 #endif