diff options
author | jbevain <jbevain@gmail.com> | 2011-07-23 21:46:20 +0400 |
---|---|---|
committer | jbevain <jbevain@gmail.com> | 2011-07-23 21:46:20 +0400 |
commit | 23ac06c1c5108ad3eac651196672c7e01b2f005d (patch) | |
tree | cf48b7580dd2f105702a6e69d0bd275f71858fc2 /Mono.Cecil | |
parent | 8b3e9bab4b7197c6020bd5a612f3db06d5e0acd3 (diff) |
Provide the ability to override the default metadata resolver implementation
Diffstat (limited to 'Mono.Cecil')
-rw-r--r-- | Mono.Cecil/AssemblyReader.cs | 3 | ||||
-rw-r--r-- | Mono.Cecil/MetadataResolver.cs | 59 | ||||
-rw-r--r-- | Mono.Cecil/ModuleDefinition.cs | 26 |
3 files changed, 64 insertions, 24 deletions
diff --git a/Mono.Cecil/AssemblyReader.cs b/Mono.Cecil/AssemblyReader.cs index 96d0e5a..d7007e1 100644 --- a/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Cecil/AssemblyReader.cs @@ -85,6 +85,9 @@ namespace Mono.Cecil { if (parameters.AssemblyResolver != null) module.assembly_resolver = parameters.AssemblyResolver; + if (parameters.MetadataResolver != null) + module.metadata_resolver = parameters.MetadataResolver; + return module; } diff --git a/Mono.Cecil/MetadataResolver.cs b/Mono.Cecil/MetadataResolver.cs index 27a215f..7b1cc84 100644 --- a/Mono.Cecil/MetadataResolver.cs +++ b/Mono.Cecil/MetadataResolver.cs @@ -41,6 +41,12 @@ namespace Mono.Cecil { AssemblyDefinition Resolve (string fullName, ReaderParameters parameters); } + public interface IMetadataResolver { + TypeDefinition Resolve (TypeReference type); + FieldDefinition Resolve (FieldReference field); + MethodDefinition Resolve (MethodReference method); + } + #if !SILVERLIGHT && !CF [Serializable] #endif @@ -68,29 +74,40 @@ namespace Mono.Cecil { #endif } - static class MetadataResolver { + public class MetadataResolver : IMetadataResolver { + + readonly IAssemblyResolver assembly_resolver; + + public IAssemblyResolver AssemblyResolver { + get { return assembly_resolver; } + } + + public MetadataResolver (IAssemblyResolver assemblyResolver) + { + assembly_resolver = assemblyResolver; + } - public static TypeDefinition Resolve (IAssemblyResolver resolver, TypeReference type) + public virtual TypeDefinition Resolve (TypeReference type) { type = type.GetElementType (); var scope = type.Scope; switch (scope.MetadataScopeType) { case MetadataScopeType.AssemblyNameReference: - var assembly = resolver.Resolve ((AssemblyNameReference) scope); + var assembly = assembly_resolver.Resolve ((AssemblyNameReference) scope); if (assembly == null) return null; - return GetType (resolver, assembly.MainModule, type); + return GetType (assembly.MainModule, type); case MetadataScopeType.ModuleDefinition: - return GetType (resolver, (ModuleDefinition) scope, type); + return GetType ((ModuleDefinition) scope, type); case MetadataScopeType.ModuleReference: var modules = type.Module.Assembly.Modules; var module_ref = (ModuleReference) scope; for (int i = 0; i < modules.Count; i++) { var netmodule = modules [i]; if (netmodule.Name == module_ref.Name) - return GetType (resolver, netmodule, type); + return GetType (netmodule, type); } break; } @@ -98,9 +115,9 @@ namespace Mono.Cecil { throw new NotSupportedException (); } - static TypeDefinition GetType (IAssemblyResolver resolver, ModuleDefinition module, TypeReference reference) + static TypeDefinition GetType (ModuleDefinition module, TypeReference reference) { - var type = GetType (module, reference); + var type = GetTypeDefinition (module, reference); if (type != null) return type; @@ -123,7 +140,7 @@ namespace Mono.Cecil { return null; } - static TypeDefinition GetType (ModuleDefinition module, TypeReference type) + static TypeDefinition GetTypeDefinition (ModuleDefinition module, TypeReference type) { if (!type.IsNested) return module.GetType (type.Namespace, type.Name); @@ -135,19 +152,19 @@ namespace Mono.Cecil { return declaring_type.GetNestedType (type.Name); } - public static FieldDefinition Resolve (IAssemblyResolver resolver, FieldReference field) + public virtual FieldDefinition Resolve (FieldReference field) { - var type = Resolve (resolver, field.DeclaringType); + var type = Resolve (field.DeclaringType); if (type == null) return null; if (!type.HasFields) return null; - return GetField (resolver, type, field); + return GetField (type, field); } - static FieldDefinition GetField (IAssemblyResolver resolver, TypeDefinition type, FieldReference reference) + FieldDefinition GetField (TypeDefinition type, FieldReference reference) { while (type != null) { var field = GetField (type.Fields, reference); @@ -157,13 +174,13 @@ namespace Mono.Cecil { if (type.BaseType == null) return null; - type = Resolve (resolver, type.BaseType); + type = Resolve (type.BaseType); } return null; } - static FieldDefinition GetField (IList<FieldDefinition> fields, FieldReference reference) + static FieldDefinition GetField (Collection<FieldDefinition> fields, FieldReference reference) { for (int i = 0; i < fields.Count; i++) { var field = fields [i]; @@ -180,9 +197,9 @@ namespace Mono.Cecil { return null; } - public static MethodDefinition Resolve (IAssemblyResolver resolver, MethodReference method) + public virtual MethodDefinition Resolve (MethodReference method) { - var type = Resolve (resolver, method.DeclaringType); + var type = Resolve (method.DeclaringType); if (type == null) return null; @@ -191,10 +208,10 @@ namespace Mono.Cecil { if (!type.HasMethods) return null; - return GetMethod (resolver, type, method); + return GetMethod (type, method); } - static MethodDefinition GetMethod (IAssemblyResolver resolver, TypeDefinition type, MethodReference reference) + MethodDefinition GetMethod (TypeDefinition type, MethodReference reference) { while (type != null) { var method = GetMethod (type.Methods, reference); @@ -204,13 +221,13 @@ namespace Mono.Cecil { if (type.BaseType == null) return null; - type = Resolve (resolver, type.BaseType); + type = Resolve (type.BaseType); } return null; } - public static MethodDefinition GetMethod (IList<MethodDefinition> methods, MethodReference reference) + public static MethodDefinition GetMethod (Collection<MethodDefinition> methods, MethodReference reference) { for (int i = 0; i < methods.Count; i++) { var method = methods [i]; diff --git a/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/ModuleDefinition.cs index 6e8aa30..20caa1b 100644 --- a/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/ModuleDefinition.cs @@ -47,6 +47,7 @@ namespace Mono.Cecil { ReadingMode reading_mode; IAssemblyResolver assembly_resolver; + IMetadataResolver metadata_resolver; Stream symbol_stream; ISymbolReaderProvider symbol_reader_provider; bool read_symbols; @@ -61,6 +62,11 @@ namespace Mono.Cecil { set { assembly_resolver = value; } } + public IMetadataResolver MetadataResolver { + get { return metadata_resolver; } + set { metadata_resolver = value; } + } + public Stream SymbolStream { get { return symbol_stream; } set { symbol_stream = value; } @@ -95,6 +101,7 @@ namespace Mono.Cecil { TargetRuntime runtime; TargetArchitecture architecture; IAssemblyResolver assembly_resolver; + IMetadataResolver metadata_resolver; public ModuleKind Kind { get { return kind; } @@ -116,6 +123,11 @@ namespace Mono.Cecil { set { assembly_resolver = value; } } + public IMetadataResolver MetadataResolver { + get { return metadata_resolver; } + set { metadata_resolver = value; } + } + public ModuleParameters () { this.kind = ModuleKind.Dll; @@ -186,6 +198,7 @@ namespace Mono.Cecil { internal ISymbolReader SymbolReader; internal IAssemblyResolver assembly_resolver; + internal IMetadataResolver metadata_resolver; internal TypeSystem type_system; readonly MetadataReader reader; @@ -269,6 +282,10 @@ namespace Mono.Cecil { get { return assembly_resolver; } } + public IMetadataResolver MetadataResolver { + get { return metadata_resolver ?? (metadata_resolver = new MetadataResolver (assembly_resolver)); } + } + public TypeSystem TypeSystem { get { return type_system ?? (type_system = TypeSystem.CreateTypeSystem (this)); } } @@ -554,17 +571,17 @@ namespace Mono.Cecil { internal FieldDefinition Resolve (FieldReference field) { - return MetadataResolver.Resolve (AssemblyResolver, field); + return MetadataResolver.Resolve (field); } internal MethodDefinition Resolve (MethodReference method) { - return MetadataResolver.Resolve (AssemblyResolver, method); + return MetadataResolver.Resolve (method); } internal TypeDefinition Resolve (TypeReference type) { - return MetadataResolver.Resolve (AssemblyResolver, type); + return MetadataResolver.Resolve (type); } #if !READ_ONLY @@ -839,6 +856,9 @@ namespace Mono.Cecil { if (parameters.AssemblyResolver != null) module.assembly_resolver = parameters.AssemblyResolver; + if (parameters.MetadataResolver != null) + module.metadata_resolver = parameters.MetadataResolver; + if (parameters.Kind != ModuleKind.NetModule) { var assembly = new AssemblyDefinition (); module.assembly = assembly; |