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;
26 using System.Diagnostics;
28 namespace IKVM.Reflection
30 public abstract class FieldInfo : MemberInfo
32 // prevent external subclasses
37 public sealed override MemberTypes MemberType
39 get { return MemberTypes.Field; }
42 public abstract FieldAttributes Attributes { get; }
43 public abstract void __GetDataFromRVA(byte[] data, int offset, int length);
44 public abstract int __FieldRVA { get; }
45 public abstract Object GetRawConstantValue();
46 internal abstract FieldSignature FieldSignature { get; }
50 get { return this.FieldSignature.FieldType; }
53 public CustomModifiers __GetCustomModifiers()
55 return this.FieldSignature.GetCustomModifiers();
58 public Type[] GetOptionalCustomModifiers()
60 return __GetCustomModifiers().GetOptional();
63 public Type[] GetRequiredCustomModifiers()
65 return __GetCustomModifiers().GetRequired();
70 get { return (Attributes & FieldAttributes.Static) != 0; }
75 get { return (Attributes & FieldAttributes.Literal) != 0; }
78 public bool IsInitOnly
80 get { return (Attributes & FieldAttributes.InitOnly) != 0; }
83 public bool IsNotSerialized
85 get { return (Attributes & FieldAttributes.NotSerialized) != 0; }
88 public bool IsSpecialName
90 get { return (Attributes & FieldAttributes.SpecialName) != 0; }
95 get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public; }
100 get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private; }
105 get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family; }
108 public bool IsFamilyOrAssembly
110 get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem; }
113 public bool IsAssembly
115 get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly; }
118 public bool IsFamilyAndAssembly
120 get { return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem; }
123 public bool IsPinvokeImpl
125 get { return (Attributes & FieldAttributes.PinvokeImpl) != 0; }
128 public virtual FieldInfo __GetFieldOnTypeDefinition()
133 internal abstract int ImportTo(Emit.ModuleBuilder module);
135 internal virtual FieldInfo BindTypeParameters(Type type)
137 return new GenericFieldInstance(this.DeclaringType.BindTypeParameters(type), this);
140 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
142 return BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
143 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static, BindingFlags.Instance);
146 internal sealed override bool BindingFlagsMatchInherited(BindingFlags flags)
148 return (Attributes & FieldAttributes.FieldAccessMask) > FieldAttributes.Private
149 && BindingFlagsMatch(IsPublic, flags, BindingFlags.Public, BindingFlags.NonPublic)
150 && BindingFlagsMatch(IsStatic, flags, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance);
153 internal sealed override MemberInfo SetReflectedType(Type type)
155 return new FieldInfoWithReflectedType(type, this);
159 sealed class FieldInfoWithReflectedType : FieldInfo
161 private readonly Type reflectedType;
162 private readonly FieldInfo field;
164 internal FieldInfoWithReflectedType(Type reflectedType, FieldInfo field)
166 Debug.Assert(reflectedType != field.DeclaringType);
167 this.reflectedType = reflectedType;
171 public override FieldAttributes Attributes
173 get { return field.Attributes; }
176 public override void __GetDataFromRVA(byte[] data, int offset, int length)
178 field.__GetDataFromRVA(data, offset, length);
181 public override int __FieldRVA
183 get { return field.__FieldRVA; }
186 public override Object GetRawConstantValue()
188 return field.GetRawConstantValue();
191 internal override FieldSignature FieldSignature
193 get { return field.FieldSignature; }
196 public override FieldInfo __GetFieldOnTypeDefinition()
198 return field.__GetFieldOnTypeDefinition();
201 internal override int ImportTo(Emit.ModuleBuilder module)
203 return field.ImportTo(module);
206 internal override FieldInfo BindTypeParameters(Type type)
208 return field.BindTypeParameters(type);
211 public override bool __IsMissing
213 get { return field.__IsMissing; }
216 public override Type DeclaringType
218 get { return field.DeclaringType; }
221 public override Type ReflectedType
223 get { return reflectedType; }
226 public override bool Equals(object obj)
228 FieldInfoWithReflectedType other = obj as FieldInfoWithReflectedType;
230 && other.reflectedType == reflectedType
231 && other.field == field;
234 public override int GetHashCode()
236 return reflectedType.GetHashCode() ^ field.GetHashCode();
239 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
241 return field.GetCustomAttributesData(attributeType);
244 public override int MetadataToken
246 get { return field.MetadataToken; }
249 public override Module Module
251 get { return field.Module; }
254 public override string Name
256 get { return field.Name; }
259 public override string ToString()
261 return field.ToString();