From ab7b057e5d73d09c6d35b92bac54fa67da03cea2 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 26 Jan 2013 23:23:39 +0100 Subject: [PATCH] CecilLoader: When loading a multi-module assembly, load the main module only. We cannot merge all the types from multi-module assemblies into the same IAssembly because duplicate type names may exist. --- .../TypeSystem/CecilLoader.cs | 74 +++++++++++-------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs b/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs index 639a5d6c90..82b03f67d3 100644 --- a/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs +++ b/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs @@ -157,31 +157,46 @@ namespace ICSharpCode.NRefactory.TypeSystem /// /// Loads the assembly definition into a project content. /// - /// IProjectContent that represents the assembly + /// Unresolved type system representing the assembly [CLSCompliant(false)] public IUnresolvedAssembly LoadAssembly(AssemblyDefinition assemblyDefinition) { if (assemblyDefinition == null) throw new ArgumentNullException("assemblyDefinition"); + return LoadModule(assemblyDefinition.MainModule); + } + + /// + /// Loads the module definition into a project content. + /// + /// Unresolved type system representing the assembly + [CLSCompliant(false)] + public IUnresolvedAssembly LoadModule(ModuleDefinition moduleDefinition) + { + if (moduleDefinition == null) + throw new ArgumentNullException("moduleDefinition"); - this.currentModule = assemblyDefinition.MainModule; + this.currentModule = moduleDefinition; // Read assembly and module attributes IList assemblyAttributes = new List(); IList moduleAttributes = new List(); - AddAttributes(assemblyDefinition, assemblyAttributes); - AddAttributes(assemblyDefinition.MainModule, moduleAttributes); + AssemblyDefinition assemblyDefinition = moduleDefinition.Assembly; + if (assemblyDefinition != null) { + AddAttributes(assemblyDefinition, assemblyAttributes); + } + AddAttributes(moduleDefinition, moduleAttributes); assemblyAttributes = interningProvider.InternList(assemblyAttributes); moduleAttributes = interningProvider.InternList(moduleAttributes); - this.currentAssembly = new CecilUnresolvedAssembly(assemblyDefinition.Name, this.DocumentationProvider); - currentAssembly.Location = assemblyDefinition.MainModule.FullyQualifiedName; + this.currentAssembly = new CecilUnresolvedAssembly(assemblyDefinition != null ? assemblyDefinition.Name.FullName : moduleDefinition.Name, this.DocumentationProvider); + currentAssembly.Location = moduleDefinition.FullyQualifiedName; currentAssembly.AssemblyAttributes.AddRange(assemblyAttributes); currentAssembly.ModuleAttributes.AddRange(assemblyAttributes); // Register type forwarders: - foreach (ExportedType type in assemblyDefinition.MainModule.ExportedTypes) { + foreach (ExportedType type in moduleDefinition.ExportedTypes) { if (type.IsForwarder) { int typeParameterCount; string ns = type.Namespace; @@ -199,25 +214,23 @@ namespace ICSharpCode.NRefactory.TypeSystem CecilLoader cecilLoaderCloneForLazyLoading = LazyLoad ? new CecilLoader(this) : null; List cecilTypeDefs = new List(); List typeDefs = new List(); - foreach (ModuleDefinition module in assemblyDefinition.Modules) { - foreach (TypeDefinition td in module.Types) { - this.CancellationToken.ThrowIfCancellationRequested(); - if (this.IncludeInternalMembers || (td.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) { - string name = td.Name; - if (name.Length == 0) - continue; - - if (this.LazyLoad) { - var t = new LazyCecilTypeDefinition(cecilLoaderCloneForLazyLoading, td); - currentAssembly.AddTypeDefinition(t); - RegisterCecilObject(t, td); - } else { - var t = CreateTopLevelTypeDefinition(td); - cecilTypeDefs.Add(td); - typeDefs.Add(t); - currentAssembly.AddTypeDefinition(t); - // The registration will happen after the members are initialized - } + foreach (TypeDefinition td in moduleDefinition.Types) { + this.CancellationToken.ThrowIfCancellationRequested(); + if (this.IncludeInternalMembers || (td.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) { + string name = td.Name; + if (name.Length == 0) + continue; + + if (this.LazyLoad) { + var t = new LazyCecilTypeDefinition(cecilLoaderCloneForLazyLoading, td); + currentAssembly.AddTypeDefinition(t); + RegisterCecilObject(t, td); + } else { + var t = CreateTopLevelTypeDefinition(td); + cecilTypeDefs.Add(td); + typeDefs.Add(t); + currentAssembly.AddTypeDefinition(t); + // The registration will happen after the members are initialized } } } @@ -276,10 +289,9 @@ namespace ICSharpCode.NRefactory.TypeSystem { readonly IDocumentationProvider documentationProvider; - public CecilUnresolvedAssembly(AssemblyNameDefinition assemblyName, IDocumentationProvider documentationProvider) - : base(assemblyName.FullName) + public CecilUnresolvedAssembly(string fullAssemblyName, IDocumentationProvider documentationProvider) + : base(fullAssemblyName) { - Debug.Assert(assemblyName != null); this.documentationProvider = documentationProvider; } @@ -299,8 +311,8 @@ namespace ICSharpCode.NRefactory.TypeSystem if (fileName == null) throw new ArgumentNullException("fileName"); var param = new ReaderParameters { AssemblyResolver = new DummyAssemblyResolver() }; - AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(fileName, param); - return LoadAssembly(asm); + ModuleDefinition module = ModuleDefinition.ReadModule(fileName, param); + return LoadModule(module); } // used to prevent Cecil from loading referenced assemblies