Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / IKVM.Reflection / PropertyInfo.cs
1 /*
2   Copyright (C) 2009-2012 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26
27 namespace IKVM.Reflection
28 {
29         public abstract class PropertyInfo : MemberInfo
30         {
31                 // prevent external subclasses
32                 internal PropertyInfo()
33                 {
34                 }
35
36                 public sealed override MemberTypes MemberType
37                 {
38                         get { return MemberTypes.Property; }
39                 }
40
41                 public abstract PropertyAttributes Attributes { get; }
42                 public abstract bool CanRead { get; }
43                 public abstract bool CanWrite { get; }
44                 public abstract MethodInfo GetGetMethod(bool nonPublic);
45                 public abstract MethodInfo GetSetMethod(bool nonPublic);
46                 public abstract MethodInfo[] GetAccessors(bool nonPublic);
47                 public abstract object GetRawConstantValue();
48                 internal abstract bool IsPublic { get; }
49                 internal abstract bool IsNonPrivate { get; }
50                 internal abstract bool IsStatic { get; }
51                 internal abstract PropertySignature PropertySignature { get; }
52
53                 private sealed class ParameterInfoImpl : ParameterInfo
54                 {
55                         private readonly PropertyInfo property;
56                         private readonly int parameter;
57
58                         internal ParameterInfoImpl(PropertyInfo property, int parameter)
59                         {
60                                 this.property = property;
61                                 this.parameter = parameter;
62                         }
63
64                         public override string Name
65                         {
66                                 get { return null; }
67                         }
68
69                         public override Type ParameterType
70                         {
71                                 get { return property.PropertySignature.GetParameter(parameter); }
72                         }
73
74                         public override ParameterAttributes Attributes
75                         {
76                                 get { return ParameterAttributes.None; }
77                         }
78
79                         public override int Position
80                         {
81                                 get { return parameter; }
82                         }
83
84                         public override object RawDefaultValue
85                         {
86                                 get { throw new InvalidOperationException(); }
87                         }
88
89                         public override CustomModifiers __GetCustomModifiers()
90                         {
91                                 return property.PropertySignature.GetParameterCustomModifiers(parameter);
92                         }
93
94                         public override MemberInfo Member
95                         {
96                                 get { return property; }
97                         }
98
99                         public override int MetadataToken
100                         {
101                                 get { return 0x08000000; }
102                         }
103
104                         internal override Module Module
105                         {
106                                 get { return property.Module; }
107                         }
108                 }
109
110                 public virtual ParameterInfo[] GetIndexParameters()
111                 {
112                         ParameterInfo[] parameters = new ParameterInfo[this.PropertySignature.ParameterCount];
113                         for (int i = 0; i < parameters.Length; i++)
114                         {
115                                 parameters[i] = new ParameterInfoImpl(this, i);
116                         }
117                         return parameters;
118                 }
119
120                 public Type PropertyType
121                 {
122                         get { return this.PropertySignature.PropertyType; }
123                 }
124
125                 public CustomModifiers __GetCustomModifiers()
126                 {
127                         return this.PropertySignature.GetCustomModifiers();
128                 }
129
130                 public Type[] GetRequiredCustomModifiers()
131                 {
132                         return __GetCustomModifiers().GetRequired();
133                 }
134
135                 public Type[] GetOptionalCustomModifiers()
136                 {
137                         return __GetCustomModifiers().GetOptional();
138                 }
139
140                 public bool IsSpecialName
141                 {
142                         get { return (Attributes & PropertyAttributes.SpecialName) != 0; }
143                 }
144
145                 public MethodInfo GetGetMethod()
146                 {
147                         return GetGetMethod(false);
148                 }
149
150                 public MethodInfo GetSetMethod()
151                 {
152                         return GetSetMethod(false);
153                 }
154
155                 public MethodInfo[] GetAccessors()
156                 {
157                         return GetAccessors(false);
158                 }
159
160                 public CallingConventions __CallingConvention
161                 {
162                         get { return this.PropertySignature.CallingConvention; }
163                 }
164
165                 internal virtual PropertyInfo BindTypeParameters(Type type)
166                 {
167                         return new GenericPropertyInfo(this.DeclaringType.BindTypeParameters(type), this);
168                 }
169
170                 public override string ToString()
171                 {
172                         return this.DeclaringType.ToString() + " " + Name;
173                 }
174
175                 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
176                 {
177                         return BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
178                                 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static, BindingFlags.Instance);
179                 }
180
181                 internal sealed override bool BindingFlagsMatchInherited(BindingFlags flags)
182                 {
183                         return IsNonPrivate
184                                 && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
185                                 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
186                 }
187
188                 internal sealed override MemberInfo SetReflectedType(Type type)
189                 {
190                         return new PropertyInfoWithReflectedType(type, this);
191                 }
192         }
193
194         sealed class PropertyInfoWithReflectedType : PropertyInfo
195         {
196                 private readonly Type reflectedType;
197                 private readonly PropertyInfo property;
198
199                 internal PropertyInfoWithReflectedType(Type reflectedType, PropertyInfo property)
200                 {
201                         this.reflectedType = reflectedType;
202                         this.property = property;
203                 }
204
205                 public override PropertyAttributes Attributes
206                 {
207                         get { return property.Attributes; }
208                 }
209
210                 public override bool CanRead
211                 {
212                         get { return property.CanRead; }
213                 }
214
215                 public override bool CanWrite
216                 {
217                         get { return property.CanWrite; }
218                 }
219
220                 public override MethodInfo GetGetMethod(bool nonPublic)
221                 {
222                         return SetReflectedType(property.GetGetMethod(nonPublic), reflectedType);
223                 }
224
225                 public override MethodInfo GetSetMethod(bool nonPublic)
226                 {
227                         return SetReflectedType(property.GetSetMethod(nonPublic), reflectedType);
228                 }
229
230                 public override MethodInfo[] GetAccessors(bool nonPublic)
231                 {
232                         return SetReflectedType(property.GetAccessors(nonPublic), reflectedType);
233                 }
234
235                 public override object GetRawConstantValue()
236                 {
237                         return property.GetRawConstantValue();
238                 }
239
240                 internal override bool IsPublic
241                 {
242                         get { return property.IsPublic; }
243                 }
244
245                 internal override bool IsNonPrivate
246                 {
247                         get { return property.IsNonPrivate; }
248                 }
249
250                 internal override bool IsStatic
251                 {
252                         get { return property.IsStatic; }
253                 }
254
255                 internal override PropertySignature PropertySignature
256                 {
257                         get { return property.PropertySignature; }
258                 }
259
260                 public override ParameterInfo[] GetIndexParameters()
261                 {
262                         ParameterInfo[] parameters = property.GetIndexParameters();
263                         for (int i = 0; i < parameters.Length; i++)
264                         {
265                                 parameters[i] = new ParameterInfoWrapper(this, parameters[i]);
266                         }
267                         return parameters;
268                 }
269
270                 internal override PropertyInfo BindTypeParameters(Type type)
271                 {
272                         return property.BindTypeParameters(type);
273                 }
274
275                 public override string ToString()
276                 {
277                         return property.ToString();
278                 }
279
280                 public override bool __IsMissing
281                 {
282                         get { return property.__IsMissing; }
283                 }
284
285                 public override Type DeclaringType
286                 {
287                         get { return property.DeclaringType; }
288                 }
289
290                 public override Type ReflectedType
291                 {
292                         get { return reflectedType; }
293                 }
294
295                 public override bool Equals(object obj)
296                 {
297                         PropertyInfoWithReflectedType other = obj as PropertyInfoWithReflectedType;
298                         return other != null
299                                 && other.reflectedType == reflectedType
300                                 && other.property == property;
301                 }
302
303                 public override int GetHashCode()
304                 {
305                         return reflectedType.GetHashCode() ^ property.GetHashCode();
306                 }
307
308                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
309                 {
310                         return property.GetCustomAttributesData(attributeType);
311                 }
312
313                 public override int MetadataToken
314                 {
315                         get { return property.MetadataToken; }
316                 }
317
318                 public override Module Module
319                 {
320                         get { return property.Module; }
321                 }
322
323                 public override string Name
324                 {
325                         get { return property.Name; }
326                 }
327         }
328 }