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 // 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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
36 using System
.Globalization
;
37 using System
.Runtime
.CompilerServices
;
38 using System
.Runtime
.InteropServices
;
40 namespace System
.Reflection
{
42 internal struct MonoPropertyInfo
{
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
,
59 ReflectedType
= 1 << 3,
60 DeclaringType
= 1 << 4,
64 internal class MonoProperty
: PropertyInfo
{
65 internal IntPtr klass
;
68 public override PropertyAttributes Attributes
{
70 MonoPropertyInfo info
;
71 MonoPropertyInfo
.get_property_info (this, out info
, PInfo
.Attributes
);
76 public override bool CanRead
{
78 MonoPropertyInfo info
;
79 MonoPropertyInfo
.get_property_info (this, out info
, PInfo
.GetMethod
);
80 return (info
.get_method
!= null);
84 public override bool CanWrite
{
86 MonoPropertyInfo info
;
87 MonoPropertyInfo
.get_property_info (this, out info
, PInfo
.SetMethod
);
88 return (info
.set_method
!= null);
92 public override Type PropertyType
{
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
;
100 ParameterInfo
[] parameters
= info
.set_method
.GetParameters();
102 return parameters
[parameters
.Length
- 1].ParameterType
;
107 public override Type ReflectedType
{
109 MonoPropertyInfo info
;
110 MonoPropertyInfo
.get_property_info (this, out info
, PInfo
.ReflectedType
);
115 public override Type DeclaringType
{
117 MonoPropertyInfo info
;
118 MonoPropertyInfo
.get_property_info (this, out info
, PInfo
.DeclaringType
);
123 public override string Name
{
125 MonoPropertyInfo info
;
126 MonoPropertyInfo
.get_property_info (this, out info
, PInfo
.Name
);
131 public override MethodInfo
[] GetAccessors (bool nonPublic
)
133 MonoPropertyInfo info
;
137 MonoPropertyInfo
.get_property_info (this, out info
, PInfo
.GetMethod
| PInfo
.SetMethod
);
138 if (info
.set_method
!= null && (nonPublic
|| info
.set_method
.IsPublic
))
140 if (info
.get_method
!= null && (nonPublic
|| info
.get_method
.IsPublic
))
143 MethodInfo
[] res
= new MethodInfo
[nget
+ nset
];
146 res
[n
++] = info
.set_method
;
148 res
[n
++] = info
.get_method
;
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
;
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
;
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
)
200 MethodInfo method
= GetGetMethod (true);
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
);
207 ret
= method
.Invoke (obj
, invokeAttr
, binder
, index
, culture
);
212 public override void SetValue (object obj
, object value, BindingFlags invokeAttr
, Binder binder
, object[] index
, CultureInfo culture
)
214 MethodInfo method
= GetSetMethod (true);
216 throw new ArgumentException ("Set Method not found for '" + Name
+ "'");
219 if (index
== null || index
.Length
== 0)
220 parms
= new object [] {value}
;
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
237 public override Type
[] OptionalCustomModifiers
{
239 throw new NotImplementedException ();
244 public override Type
[] RequiredCustomModifiers
{
246 throw new NotImplementedException ();