2 Copyright (C) 2009 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;
28 using IKVM.Reflection.Metadata;
30 namespace IKVM.Reflection.Reader
32 sealed class FieldDefImpl : FieldInfo
34 private readonly ModuleReader module;
35 private readonly TypeDefImpl declaringType;
36 private readonly int index;
37 private FieldSignature lazyFieldSig;
39 internal FieldDefImpl(ModuleReader module, TypeDefImpl declaringType, int index)
42 this.declaringType = declaringType;
46 public override FieldAttributes Attributes
48 get { return (FieldAttributes)module.Field.records[index].Flags; }
51 public override Type DeclaringType
53 get { return declaringType.IsModulePseudoType ? null : declaringType; }
56 public override string Name
58 get { return module.GetString(module.Field.records[index].Name); }
61 public override string ToString()
63 return this.FieldType.Name + " " + this.Name;
66 public override Module Module
68 get { return module; }
71 public override int MetadataToken
73 get { return (FieldTable.Index << 24) + index + 1; }
76 public override object GetRawConstantValue()
78 return module.Constant.GetRawConstantValue(module, this.MetadataToken);
81 public override void __GetDataFromRVA(byte[] data, int offset, int length)
83 int rva = this.__FieldRVA;
86 // C++ assemblies can have fields that have an RVA that is zero
87 Array.Clear(data, offset, length);
90 module.__ReadDataFromRVA(rva, data, offset, length);
93 public override int __FieldRVA
97 foreach (int i in module.FieldRVA.Filter(index + 1))
99 return module.FieldRVA.records[i].RVA;
101 throw new InvalidOperationException();
105 public override bool __TryGetFieldOffset(out int offset)
107 foreach (int i in this.Module.FieldLayout.Filter(index + 1))
109 offset = this.Module.FieldLayout.records[i].Offset;
116 internal override FieldSignature FieldSignature
118 get { return lazyFieldSig ?? (lazyFieldSig = FieldSignature.ReadSig(module, module.GetBlob(module.Field.records[index].Signature), declaringType)); }
121 internal override int ImportTo(Emit.ModuleBuilder module)
123 return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
126 internal override int GetCurrentToken()
128 return this.MetadataToken;
131 internal override bool IsBaked