From 862e9500dbf5424585c61315d2a9822d586e7f0f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 14 Feb 2012 18:44:09 +0100 Subject: [PATCH] When the specified reflection name is not assembly-qualified, ReflectionHelper.ParseReflectionName() will now produce an ITypeReference that looks in all referenced assemblies (not just in CurrentAssembly). --- .../DefaultResolvedTypeDefinition.cs | 2 +- .../Implementation/GetClassTypeReference.cs | 49 ++++++++++++++++--- .../TypeSystem/ReflectionHelper.cs | 8 +-- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs index b2f2a48d18..db8a342c08 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultResolvedTypeDefinition.cs @@ -461,7 +461,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation if (asm != null) asmRef = new DefaultAssemblyReference(asm.AssemblyName); else - asmRef = DefaultAssemblyReference.CurrentAssembly; + asmRef = null; return new GetClassTypeReference(asmRef, this.Namespace, this.Name, this.TypeParameterCount); } } diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs index d71fd69b76..a49ddda0ba 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs @@ -17,6 +17,7 @@ // DEALINGS IN THE SOFTWARE. using System; +using System.Linq; using System.Linq.Expressions; using ICSharpCode.NRefactory.Utils; @@ -33,12 +34,32 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation int typeParameterCount; /// - /// Creates a new GetClassTypeReference that searches a type in the specified assembly. + /// Creates a new GetClassTypeReference that searches a top-level type. /// + /// The namespace name containing the type, e.g. "System.Collections.Generic". + /// The name of the type, e.g. "List". + /// The number of type parameters, (e.g. 1 for List<T>). + public GetClassTypeReference(string nameSpace, string name, int typeParameterCount) + { + if (nameSpace == null) + throw new ArgumentNullException("nameSpace"); + if (name == null) + throw new ArgumentNullException("name"); + this.nameSpace = nameSpace; + this.name = name; + this.typeParameterCount = typeParameterCount; + } + + /// + /// Creates a new GetClassTypeReference that searches a top-level type in the specified assembly. + /// + /// A reference to the assembly containing this type. + /// If this parameter is null, the GetClassTypeReference will search in all assemblies belonging to the ICompilation. + /// The namespace name containing the type, e.g. "System.Collections.Generic". + /// The name of the type, e.g. "List". + /// The number of type parameters, (e.g. 1 for List<T>). public GetClassTypeReference(IAssemblyReference assembly, string nameSpace, string name, int typeParameterCount) { - if (assembly == null) - throw new ArgumentNullException("assembly"); if (nameSpace == null) throw new ArgumentNullException("nameSpace"); if (name == null) @@ -59,20 +80,32 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation if (context == null) throw new ArgumentNullException("context"); - IAssembly asm = assembly.Resolve(context); IType type = null; - if (asm != null) { - type = asm.GetTypeDefinition(nameSpace, name, typeParameterCount); + if (assembly == null) { + var compilation = context.Compilation; + foreach (var asm in new[] { context.CurrentAssembly, compilation.MainAssembly }.Concat(compilation.ReferencedAssemblies)) { + if (asm != null) { + type = asm.GetTypeDefinition(nameSpace, name, typeParameterCount); + if (type != null) + return type; + } + } + } else { + IAssembly asm = assembly.Resolve(context); + if (asm != null) { + type = asm.GetTypeDefinition(nameSpace, name, typeParameterCount); + } } return type ?? new UnknownType(nameSpace, name, typeParameterCount); } public override string ToString() { + string asmSuffix = (assembly != null ? ", " + assembly.ToString() : null); if (typeParameterCount == 0) - return BuildQualifiedName(nameSpace, name); + return BuildQualifiedName(nameSpace, name) + asmSuffix; else - return BuildQualifiedName(nameSpace, name) + "`" + typeParameterCount; + return BuildQualifiedName(nameSpace, name) + "`" + typeParameterCount + asmSuffix; } static string BuildQualifiedName (string name1, string name2) diff --git a/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs b/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs index f01fb8117c..47a2f89c38 100644 --- a/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs +++ b/ICSharpCode.NRefactory/TypeSystem/ReflectionHelper.cs @@ -211,11 +211,11 @@ namespace ICSharpCode.NRefactory.TypeSystem /// /// If the type is open (contains type parameters '`0' or '``0'), /// an with the appropriate CurrentTypeDefinition/CurrentMember is required - /// to resolve the type reference. + /// to resolve the reference to the ITypeParameter. /// For looking up closed, assembly qualified type names, the root type resolve context for the compilation /// is sufficient. - /// When looking up a type name that isn't assembly qualified, the type reference only looks - /// in the . + /// When looking up a type name that isn't assembly qualified, the type reference will look in + /// first, and . /// public static ITypeReference ParseReflectionName(string reflectionTypeName) { @@ -354,7 +354,7 @@ namespace ICSharpCode.NRefactory.TypeSystem if (assemblyName != null) { assemblyReference = new DefaultAssemblyReference(assemblyName); } else { - assemblyReference = DefaultAssemblyReference.CurrentAssembly; + assemblyReference = null; } int pos = typeName.LastIndexOf('.'); if (pos < 0)