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 IKVM.Reflection.Metadata;
27 using IKVM.Reflection.Reader;
29 namespace IKVM.Reflection
31 public sealed class RawModule : IDisposable
33 private readonly ModuleReader module;
34 private readonly bool isManifestModule;
35 private bool imported;
37 internal RawModule(ModuleReader module)
40 this.isManifestModule = module.Assembly != null;
43 public string Location
45 get { return module.FullyQualifiedName; }
48 public bool IsManifestModule
50 get { return isManifestModule; }
53 public Guid ModuleVersionId
55 get { return module.ModuleVersionId; }
58 private void CheckManifestModule()
60 if (!IsManifestModule)
62 throw new BadImageFormatException("Module does not contain a manifest");
66 public AssemblyName GetAssemblyName()
68 CheckManifestModule();
69 return module.Assembly.GetName();
72 public AssemblyName[] GetReferencedAssemblies()
74 return module.__GetReferencedAssemblies();
81 module.stream.Dispose();
85 internal AssemblyReader ToAssembly()
89 throw new InvalidOperationException();
92 return (AssemblyReader)module.Assembly;
95 internal Module ToModule(Assembly assembly)
97 if (module.Assembly != null)
99 throw new InvalidOperationException();
102 module.SetAssembly(assembly);
107 public abstract class Module : ICustomAttributeProvider
109 internal readonly Universe universe;
110 internal readonly ModuleTable ModuleTable = new ModuleTable();
111 internal readonly TypeRefTable TypeRef = new TypeRefTable();
112 internal readonly TypeDefTable TypeDef = new TypeDefTable();
113 internal readonly FieldPtrTable FieldPtr = new FieldPtrTable();
114 internal readonly FieldTable Field = new FieldTable();
115 internal readonly MemberRefTable MemberRef = new MemberRefTable();
116 internal readonly ConstantTable Constant = new ConstantTable();
117 internal readonly CustomAttributeTable CustomAttribute = new CustomAttributeTable();
118 internal readonly FieldMarshalTable FieldMarshal = new FieldMarshalTable();
119 internal readonly DeclSecurityTable DeclSecurity = new DeclSecurityTable();
120 internal readonly ClassLayoutTable ClassLayout = new ClassLayoutTable();
121 internal readonly FieldLayoutTable FieldLayout = new FieldLayoutTable();
122 internal readonly ParamPtrTable ParamPtr = new ParamPtrTable();
123 internal readonly ParamTable Param = new ParamTable();
124 internal readonly InterfaceImplTable InterfaceImpl = new InterfaceImplTable();
125 internal readonly StandAloneSigTable StandAloneSig = new StandAloneSigTable();
126 internal readonly EventMapTable EventMap = new EventMapTable();
127 internal readonly EventPtrTable EventPtr = new EventPtrTable();
128 internal readonly EventTable Event = new EventTable();
129 internal readonly PropertyMapTable PropertyMap = new PropertyMapTable();
130 internal readonly PropertyPtrTable PropertyPtr = new PropertyPtrTable();
131 internal readonly PropertyTable Property = new PropertyTable();
132 internal readonly MethodSemanticsTable MethodSemantics = new MethodSemanticsTable();
133 internal readonly MethodImplTable MethodImpl = new MethodImplTable();
134 internal readonly ModuleRefTable ModuleRef = new ModuleRefTable();
135 internal readonly TypeSpecTable TypeSpec = new TypeSpecTable();
136 internal readonly ImplMapTable ImplMap = new ImplMapTable();
137 internal readonly FieldRVATable FieldRVA = new FieldRVATable();
138 internal readonly AssemblyTable AssemblyTable = new AssemblyTable();
139 internal readonly AssemblyRefTable AssemblyRef = new AssemblyRefTable();
140 internal readonly MethodPtrTable MethodPtr = new MethodPtrTable();
141 internal readonly MethodDefTable MethodDef = new MethodDefTable();
142 internal readonly NestedClassTable NestedClass = new NestedClassTable();
143 internal readonly FileTable File = new FileTable();
144 internal readonly ExportedTypeTable ExportedType = new ExportedTypeTable();
145 internal readonly ManifestResourceTable ManifestResource = new ManifestResourceTable();
146 internal readonly GenericParamTable GenericParam = new GenericParamTable();
147 internal readonly MethodSpecTable MethodSpec = new MethodSpecTable();
148 internal readonly GenericParamConstraintTable GenericParamConstraint = new GenericParamConstraintTable();
150 protected Module(Universe universe)
152 this.universe = universe;
155 internal Table[] GetTables()
157 Table[] tables = new Table[64];
158 tables[ModuleTable.Index] = ModuleTable;
159 tables[TypeRefTable.Index] = TypeRef;
160 tables[TypeDefTable.Index] = TypeDef;
161 tables[FieldPtrTable.Index] = FieldPtr;
162 tables[FieldTable.Index] = Field;
163 tables[MemberRefTable.Index] = MemberRef;
164 tables[ConstantTable.Index] = Constant;
165 tables[CustomAttributeTable.Index] = CustomAttribute;
166 tables[FieldMarshalTable.Index] = FieldMarshal;
167 tables[DeclSecurityTable.Index] = DeclSecurity;
168 tables[ClassLayoutTable.Index] = ClassLayout;
169 tables[FieldLayoutTable.Index] = FieldLayout;
170 tables[ParamPtrTable.Index] = ParamPtr;
171 tables[ParamTable.Index] = Param;
172 tables[InterfaceImplTable.Index] = InterfaceImpl;
173 tables[StandAloneSigTable.Index] = StandAloneSig;
174 tables[EventMapTable.Index] = EventMap;
175 tables[EventPtrTable.Index] = EventPtr;
176 tables[EventTable.Index] = Event;
177 tables[PropertyMapTable.Index] = PropertyMap;
178 tables[PropertyPtrTable.Index] = PropertyPtr;
179 tables[PropertyTable.Index] = Property;
180 tables[MethodSemanticsTable.Index] = MethodSemantics;
181 tables[MethodImplTable.Index] = MethodImpl;
182 tables[ModuleRefTable.Index] = ModuleRef;
183 tables[TypeSpecTable.Index] = TypeSpec;
184 tables[ImplMapTable.Index] = ImplMap;
185 tables[FieldRVATable.Index] = FieldRVA;
186 tables[AssemblyTable.Index] = AssemblyTable;
187 tables[AssemblyRefTable.Index] = AssemblyRef;
188 tables[MethodPtrTable.Index] = MethodPtr;
189 tables[MethodDefTable.Index] = MethodDef;
190 tables[NestedClassTable.Index] = NestedClass;
191 tables[FileTable.Index] = File;
192 tables[ExportedTypeTable.Index] = ExportedType;
193 tables[ManifestResourceTable.Index] = ManifestResource;
194 tables[GenericParamTable.Index] = GenericParam;
195 tables[MethodSpecTable.Index] = MethodSpec;
196 tables[GenericParamConstraintTable.Index] = GenericParamConstraint;
200 public virtual void __GetDataDirectoryEntry(int index, out int rva, out int length)
202 throw new NotSupportedException();
205 public virtual long __RelativeVirtualAddressToFileOffset(int rva)
207 throw new NotSupportedException();
210 public virtual bool __GetSectionInfo(int rva, out string name, out int characteristics)
212 throw new NotSupportedException();
215 public virtual int __ReadDataFromRVA(int rva, byte[] data, int offset, int length)
217 throw new NotSupportedException();
220 public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
222 throw new NotSupportedException();
225 public virtual int __Subsystem
227 get { throw new NotSupportedException(); }
230 public FieldInfo GetField(string name)
232 return GetField(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
235 public FieldInfo GetField(string name, BindingFlags bindingFlags)
237 return IsResource() ? null : GetModuleType().GetField(name, bindingFlags | BindingFlags.DeclaredOnly);
240 public FieldInfo[] GetFields()
242 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
245 public FieldInfo[] GetFields(BindingFlags bindingFlags)
247 return IsResource() ? Empty<FieldInfo>.Array : GetModuleType().GetFields(bindingFlags | BindingFlags.DeclaredOnly);
250 public MethodInfo GetMethod(string name)
252 return IsResource() ? null : GetModuleType().GetMethod(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
255 public MethodInfo GetMethod(string name, Type[] types)
257 return IsResource() ? null : GetModuleType().GetMethod(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, types, null);
260 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
262 return IsResource() ? null : GetModuleType().GetMethod(name, bindingAttr | BindingFlags.DeclaredOnly, binder, callConv, types, modifiers);
265 public MethodInfo[] GetMethods()
267 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
270 public MethodInfo[] GetMethods(BindingFlags bindingFlags)
272 return IsResource() ? Empty<MethodInfo>.Array : GetModuleType().GetMethods(bindingFlags | BindingFlags.DeclaredOnly);
275 public ConstructorInfo __ModuleInitializer
277 get { return IsResource() ? null : GetModuleType().TypeInitializer; }
280 public virtual byte[] ResolveSignature(int metadataToken)
282 throw new NotSupportedException();
285 public virtual __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
287 throw new NotSupportedException();
290 public int MetadataToken
292 get { return IsResource() ? 0 : 1; }
295 public abstract int MDStreamVersion { get ;}
296 public abstract Assembly Assembly { get; }
297 public abstract string FullyQualifiedName { get; }
298 public abstract string Name { get; }
299 public abstract Guid ModuleVersionId { get; }
300 public abstract Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
301 public abstract MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
302 public abstract FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
303 public abstract MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
305 public abstract string ResolveString(int metadataToken);
306 public abstract Type[] __ResolveOptionalParameterTypes(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments, out CustomModifiers[] customModifiers);
307 public abstract string ScopeName { get; }
309 internal abstract void GetTypesImpl(List<Type> list);
311 internal abstract Type FindType(TypeName name);
312 internal abstract Type FindTypeIgnoreCase(TypeName lowerCaseName);
314 [Obsolete("Please use __ResolveOptionalParameterTypes(int, Type[], Type[], out CustomModifiers[]) instead.")]
315 public Type[] __ResolveOptionalParameterTypes(int metadataToken)
317 CustomModifiers[] dummy;
318 return __ResolveOptionalParameterTypes(metadataToken, null, null, out dummy);
321 public Type GetType(string className)
323 return GetType(className, false, false);
326 public Type GetType(string className, bool ignoreCase)
328 return GetType(className, false, ignoreCase);
331 public Type GetType(string className, bool throwOnError, bool ignoreCase)
333 TypeNameParser parser = TypeNameParser.Parse(className, throwOnError);
338 if (parser.AssemblyName != null)
342 throw new ArgumentException("Type names passed to Module.GetType() must not specify an assembly.");
349 TypeName typeName = TypeName.Split(TypeNameParser.Unescape(parser.FirstNamePart));
350 Type type = ignoreCase
351 ? FindTypeIgnoreCase(typeName.ToLowerInvariant())
352 : FindType(typeName);
353 if (type == null && __IsMissing)
355 throw new MissingModuleException((MissingModule)this);
357 return parser.Expand(type, this.Assembly, throwOnError, className, false, ignoreCase);
360 public Type[] GetTypes()
362 List<Type> list = new List<Type>();
364 return list.ToArray();
367 public Type[] FindTypes(TypeFilter filter, object filterCriteria)
369 List<Type> list = new List<Type>();
370 foreach (Type type in GetTypes())
372 if (filter(type, filterCriteria))
377 return list.ToArray();
380 public virtual bool IsResource()
385 public Type ResolveType(int metadataToken)
387 return ResolveType(metadataToken, null, null);
390 public MethodBase ResolveMethod(int metadataToken)
392 return ResolveMethod(metadataToken, null, null);
395 public FieldInfo ResolveField(int metadataToken)
397 return ResolveField(metadataToken, null, null);
400 public MemberInfo ResolveMember(int metadataToken)
402 return ResolveMember(metadataToken, null, null);
405 public bool IsDefined(Type attributeType, bool inherit)
407 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
410 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
412 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
415 public virtual IList<CustomAttributeData> __GetPlaceholderAssemblyCustomAttributes(bool multiple, bool security)
417 return Empty<CustomAttributeData>.Array;
420 public abstract AssemblyName[] __GetReferencedAssemblies();
422 public virtual void __ResolveReferencedAssemblies(Assembly[] assemblies)
424 throw new NotSupportedException();
427 public abstract string[] __GetReferencedModules();
429 public abstract Type[] __GetReferencedTypes();
431 public abstract Type[] __GetExportedTypes();
433 public virtual bool __IsMissing
435 get { return false; }
438 public long __ImageBase
440 get { return GetImageBaseImpl(); }
443 protected abstract long GetImageBaseImpl();
445 public long __StackReserve
447 get { return GetStackReserveImpl(); }
450 protected abstract long GetStackReserveImpl();
452 public int __FileAlignment
454 get { return GetFileAlignmentImpl(); }
457 protected abstract int GetFileAlignmentImpl();
459 public DllCharacteristics __DllCharacteristics
461 get { return GetDllCharacteristicsImpl(); }
464 protected abstract DllCharacteristics GetDllCharacteristicsImpl();
466 public virtual byte[] __ModuleHash
468 get { throw new NotSupportedException(); }
471 public virtual int __EntryPointRVA
473 get { throw new NotSupportedException(); }
476 public virtual int __EntryPointToken
478 get { throw new NotSupportedException(); }
481 public virtual string __ImageRuntimeVersion
483 get { throw new NotSupportedException(); }
486 public IEnumerable<CustomAttributeData> __EnumerateCustomAttributeTable()
488 List<CustomAttributeData> list = new List<CustomAttributeData>(CustomAttribute.RowCount);
489 for (int i = 0; i < CustomAttribute.RowCount; i++)
491 list.Add(new CustomAttributeData(this, i));
497 public List<CustomAttributeData> __GetCustomAttributesFor(int token)
499 return GetCustomAttributes(token, null);
502 internal abstract Type GetModuleType();
504 internal abstract ByteReader GetBlob(int blobIndex);
506 internal virtual IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
508 return GetCustomAttributes(0x00000001, attributeType);
511 internal List<CustomAttributeData> GetCustomAttributes(int metadataToken, Type attributeType)
513 List<CustomAttributeData> list = new List<CustomAttributeData>();
514 foreach (int i in CustomAttribute.Filter(metadataToken))
516 if (attributeType == null)
518 list.Add(new CustomAttributeData(this, i));
522 ConstructorInfo constructor = (ConstructorInfo)ResolveMethod(CustomAttribute.records[i].Type);
523 if (attributeType.IsAssignableFrom(constructor.DeclaringType))
525 list.Add(new CustomAttributeData(this, i));
532 internal IList<CustomAttributeData> GetDeclarativeSecurity(int metadataToken)
534 List<CustomAttributeData> list = new List<CustomAttributeData>();
535 foreach (int i in DeclSecurity.Filter(metadataToken))
537 CustomAttributeData.ReadDeclarativeSecurity(this, i, list);
542 internal virtual void Dispose()
546 internal virtual void ExportTypes(int fileToken, IKVM.Reflection.Emit.ModuleBuilder manifestModule)
551 abstract class NonPEModule : Module
553 protected NonPEModule(Universe universe)
558 protected virtual Exception InvalidOperationException()
560 return new InvalidOperationException();
563 protected virtual Exception NotSupportedException()
565 return new NotSupportedException();
568 protected virtual Exception ArgumentOutOfRangeException()
570 return new ArgumentOutOfRangeException();
573 internal sealed override Type GetModuleType()
575 throw InvalidOperationException();
578 internal sealed override ByteReader GetBlob(int blobIndex)
580 throw InvalidOperationException();
583 public sealed override AssemblyName[] __GetReferencedAssemblies()
585 throw NotSupportedException();
588 public sealed override string[] __GetReferencedModules()
590 throw NotSupportedException();
593 public override Type[] __GetReferencedTypes()
595 throw NotSupportedException();
598 public override Type[] __GetExportedTypes()
600 throw NotSupportedException();
603 protected sealed override long GetImageBaseImpl()
605 throw NotSupportedException();
608 protected sealed override long GetStackReserveImpl()
610 throw NotSupportedException();
613 protected sealed override int GetFileAlignmentImpl()
615 throw NotSupportedException();
618 protected override DllCharacteristics GetDllCharacteristicsImpl()
620 throw NotSupportedException();
623 public sealed override Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
625 throw ArgumentOutOfRangeException();
628 public sealed override MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
630 throw ArgumentOutOfRangeException();
633 public sealed override FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
635 throw ArgumentOutOfRangeException();
638 public sealed override MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
640 throw ArgumentOutOfRangeException();
643 public sealed override string ResolveString(int metadataToken)
645 throw ArgumentOutOfRangeException();
648 public sealed override Type[] __ResolveOptionalParameterTypes(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments, out CustomModifiers[] customModifiers)
650 throw ArgumentOutOfRangeException();
654 public delegate bool TypeFilter(Type m, object filterCriteria);
655 public delegate bool MemberFilter(MemberInfo m, object filterCriteria);