2 Copyright (C) 2009-2012 Jeroen Frijters
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.
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:
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.
25 using System.Collections.Generic;
27 namespace IKVM.Reflection
29 public abstract class PropertyInfo : MemberInfo
31 // prevent external subclasses
32 internal PropertyInfo()
36 public sealed override MemberTypes MemberType
38 get { return MemberTypes.Property; }
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; }
53 private sealed class ParameterInfoImpl : ParameterInfo
55 private readonly PropertyInfo property;
56 private readonly int parameter;
58 internal ParameterInfoImpl(PropertyInfo property, int parameter)
60 this.property = property;
61 this.parameter = parameter;
64 public override string Name
69 public override Type ParameterType
71 get { return property.PropertySignature.GetParameter(parameter); }
74 public override ParameterAttributes Attributes
76 get { return ParameterAttributes.None; }
79 public override int Position
81 get { return parameter; }
84 public override object RawDefaultValue
86 get { throw new InvalidOperationException(); }
89 public override CustomModifiers __GetCustomModifiers()
91 return property.PropertySignature.GetParameterCustomModifiers(parameter);
94 public override MemberInfo Member
96 get { return property; }
99 public override int MetadataToken
101 get { return 0x08000000; }
104 internal override Module Module
106 get { return property.Module; }
110 public virtual ParameterInfo[] GetIndexParameters()
112 ParameterInfo[] parameters = new ParameterInfo[this.PropertySignature.ParameterCount];
113 for (int i = 0; i < parameters.Length; i++)
115 parameters[i] = new ParameterInfoImpl(this, i);
120 public Type PropertyType
122 get { return this.PropertySignature.PropertyType; }
125 public CustomModifiers __GetCustomModifiers()
127 return this.PropertySignature.GetCustomModifiers();
130 public Type[] GetRequiredCustomModifiers()
132 return __GetCustomModifiers().GetRequired();
135 public Type[] GetOptionalCustomModifiers()
137 return __GetCustomModifiers().GetOptional();
140 public bool IsSpecialName
142 get { return (Attributes & PropertyAttributes.SpecialName) != 0; }
145 public MethodInfo GetGetMethod()
147 return GetGetMethod(false);
150 public MethodInfo GetSetMethod()
152 return GetSetMethod(false);
155 public MethodInfo[] GetAccessors()
157 return GetAccessors(false);
160 public CallingConventions __CallingConvention
162 get { return this.PropertySignature.CallingConvention; }
165 internal virtual PropertyInfo BindTypeParameters(Type type)
167 return new GenericPropertyInfo(this.DeclaringType.BindTypeParameters(type), this);
170 public override string ToString()
172 return this.DeclaringType.ToString() + " " + Name;
175 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
177 return BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
178 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static, BindingFlags.Instance);
181 internal sealed override bool BindingFlagsMatchInherited(BindingFlags flags)
184 && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
185 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
188 internal sealed override MemberInfo SetReflectedType(Type type)
190 return new PropertyInfoWithReflectedType(type, this);
194 sealed class PropertyInfoWithReflectedType : PropertyInfo
196 private readonly Type reflectedType;
197 private readonly PropertyInfo property;
199 internal PropertyInfoWithReflectedType(Type reflectedType, PropertyInfo property)
201 this.reflectedType = reflectedType;
202 this.property = property;
205 public override PropertyAttributes Attributes
207 get { return property.Attributes; }
210 public override bool CanRead
212 get { return property.CanRead; }
215 public override bool CanWrite
217 get { return property.CanWrite; }
220 public override MethodInfo GetGetMethod(bool nonPublic)
222 return SetReflectedType(property.GetGetMethod(nonPublic), reflectedType);
225 public override MethodInfo GetSetMethod(bool nonPublic)
227 return SetReflectedType(property.GetSetMethod(nonPublic), reflectedType);
230 public override MethodInfo[] GetAccessors(bool nonPublic)
232 return SetReflectedType(property.GetAccessors(nonPublic), reflectedType);
235 public override object GetRawConstantValue()
237 return property.GetRawConstantValue();
240 internal override bool IsPublic
242 get { return property.IsPublic; }
245 internal override bool IsNonPrivate
247 get { return property.IsNonPrivate; }
250 internal override bool IsStatic
252 get { return property.IsStatic; }
255 internal override PropertySignature PropertySignature
257 get { return property.PropertySignature; }
260 public override ParameterInfo[] GetIndexParameters()
262 ParameterInfo[] parameters = property.GetIndexParameters();
263 for (int i = 0; i < parameters.Length; i++)
265 parameters[i] = new ParameterInfoWrapper(this, parameters[i]);
270 internal override PropertyInfo BindTypeParameters(Type type)
272 return property.BindTypeParameters(type);
275 public override string ToString()
277 return property.ToString();
280 public override bool __IsMissing
282 get { return property.__IsMissing; }
285 public override Type DeclaringType
287 get { return property.DeclaringType; }
290 public override Type ReflectedType
292 get { return reflectedType; }
295 public override bool Equals(object obj)
297 PropertyInfoWithReflectedType other = obj as PropertyInfoWithReflectedType;
299 && other.reflectedType == reflectedType
300 && other.property == property;
303 public override int GetHashCode()
305 return reflectedType.GetHashCode() ^ property.GetHashCode();
308 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
310 return property.GetCustomAttributesData(attributeType);
313 public override int MetadataToken
315 get { return property.MetadataToken; }
318 public override Module Module
320 get { return property.Module; }
323 public override string Name
325 get { return property.Name; }